Search code examples
actionscript-3airgeolocationcoordinatesmapquest

Update the POI icon position


I want to add an image on the exact coordinates given by the GPS.

I've managed to do so, but everytime the coordinates are updated, it adds a new image (and simply don't update the coordinates of the current image).

Here's what I did :

if (Geolocation.isSupported){
my_geo.addEventListener(GeolocationEvent.UPDATE, onGeoUpdate);
} 

function onGeoUpdate(e:GeolocationEvent):void{
    trace("OngeoUpdate");
     [Embed (source="rond3.png")] 
            var rondImg:Class;
my_geo.removeEventListener(GeolocationEvent.UPDATE, onGeoUpdate);

              var myIcon:MapIcon = new MapIcon();

                myIcon.setImage(new rondImg());
              embeddedIconPoi = new Poi(new LatLng(e.latitude, e.longitude));
              embeddedIconPoi.rolloverAndInfoTitleText = "You're here";
              embeddedIconPoi.icon = myIcon;

              rondColl.add(embeddedIconPoi);
          myMap.addShapeCollection(rondColl);
}

I suppose it's because I put my variable in the onGeoUpdate function so everytime it is called, it's creating a new POI.

So I've tried that :

    if (Geolocation.isSupported){

my_geo.addEventListener(GeolocationEvent.UPDATE, onGeoUpdate);
            [Embed (source="rond3.png")] 
            var rondImg:Class;
            var myIcon:MapIcon = new MapIcon();
            rondColl.add(embeddedIconPoi);
            myIcon.setImage(new rondImg());         
            embeddedIconPoi = new Poi(new LatLng());
            embeddedIconPoi.rolloverAndInfoTitleText = "You're here";
            embeddedIconPoi.icon = myIcon;
          myMap.addShapeCollection(rondColl);
} 

function onGeoUpdate(e:GeolocationEvent):void{
    trace("OngeoUpdate");
    Poi(new LatLng(e.latitude, e.longitude))

}

but it is not working (Error #1034: Type Coercion failed: cannot convert com.mapquest::LatLng@916bfd1 to com.mapquest.tilemap.pois.Poi.)

Do you know what's wrong ?


EDIT

Here's what I have imported in my code :

import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.display.StageScaleMode;
    import com.mapquest.tilemap.*;
    import com.mapquest.LatLng;
    import com.mapquest.tilemap.pois.*;
import com.mapquest.tilemap.overlays.*;
import com.mapquest.tilemap.pois.Poi;

                import com.mapquest.tilemap.pois.MapIcon;

        import flash.sensors.Geolocation; 
import flash.events.GeolocationEvent;
            import com.mapquest.tilemap.pois.PoiEvent;


    import com.mapquest.tilemap.pois.Poi;
    import com.mapquest.services.directions.Directions;
import com.mapquest.services.directions.DirectionsConstants;
import com.mapquest.services.directions.DirectionsEvent;
import com.mapquest.services.geocode.Geocoder;
import com.mapquest.services.geocode.*;
import com.mapquest.services.geocode.GeocoderEvent;
import com.mapquest.services.geocode.GeocoderLocation;
import com.mapquest.tilemap.Size;
import flash.display.Bitmap;
import com.mapquest.tilemap.TileMap;
import com.mapquest.tilemap.controls.inputdevice.MouseWheelZoomControl;
import com.mapquest.tilemap.controls.shadymeadow.SMLargeZoomControl;
 import com.mapquest.tilemap.controls.shadymeadow.SMViewControl;

Solution

  • You're right. It's because you put your variable in the onGeoUpdate() function. I've never used geolocation before, but you should try to set your icon outside of onGeoUpdate(), and only update the coordinates within the function. Something like this might work:

    [Embed (source="rond3.png")] 
    var rondImg:Class;
    
    var myIcon:MapIcon = new MapIcon();
    myIcon.setImage(new rondImg());
    
    embeddedIconPoi = new Poi(new LatLng());
    embeddedIconPoi.rolloverAndInfoTitleText = "You're here";
    embeddedIconPoi.icon = myIcon;
    
    rondColl.add(embeddedIconPoi);
    myMap.addShapeCollection(rondColl);
    
    if (Geolocation.isSupported){
        my_geo.addEventListener(GeolocationEvent.UPDATE, onGeoUpdate);
    } 
    
    function onGeoUpdate(e:GeolocationEvent):void{
        embeddedIconPoi.latLng.lat = e.latitude;
        embeddedIconPoi.latLng.lng = e.longitude;
    }
    

    If you need more info, check the docs https://developer.mapquest.com/content/as3/v5/5.5.0/documentation/devref/com/mapquest/tilemap/pois/Poi.html#Poi()