Unable to init NativeMap again after destroying it
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" title="Map" xmlns:nativemaps="com.distriqt.extension.nativemaps.*" creationComplete="init(event)">
<fx:Script>
<![CDATA[
import com.distriqt.extension.nativemaps.NativeMaps;
import com.distriqt.extension.nativemaps.objects.CustomMarkerIcon;
import com.distriqt.extension.nativemaps.objects.LatLng;
import com.distriqt.extension.nativemaps.objects.MapMarker;
import com.distriqt.extension.nativemaps.objects.MapOptions;
import com.distriqt.extension.nativemaps.objects.MapType;
import mx.events.FlexEvent;
[Embed(source="i/custom-marker.png")]
public var CustomMarker:Class;
protected var _created:Boolean = false;
protected var _count:int = 0;
protected var _buttons:Array;
public var mapTypes:Array = [ MapType.MAP_TYPE_NORMAL, MapType.MAP_TYPE_SATELLITE, MapType.MAP_TYPE_TERRAIN, MapType.MAP_TYPE_HYBRID ];
protected function init(event:FlexEvent):void
{
if (NativeMaps.isSupported){
NativeMaps.init("MYKEY");
NativeMaps.service.addEventListener(Event.ACTIVATE, mapReady);
var options:MapOptions = NativeMaps.service.mapOptions;
options.compassEnabled = true;
options.zoomControlsEnabled = true;
options.zoomGesturesEnabled = true;
options.rotateGesturesEnabled = true;
options.myLocationButtonEnabled = true;
NativeMaps.service.setMapOptions(options);
NativeMaps.service.createMap(Capabilities.screenResolutionX, Capabilities.screenResolutionY-105,0,100);
}
}
protected function mapReady(event:Event):void{
addMarker("Home", 38.032131, -87.503195);
centerMap(38.032131,-87.503195);
NativeMaps.service.showUserLocation(true);
}
protected function removeMarkers():void{
NativeMaps.service.clearMap();
}
protected function addMarker(Name:String,lat:Number,lon:Number):void{
//var bI:BitmapImage = new BitmapImage();
//bI.source = Info;
//var icon:CustomMarkerIcon = new CustomMarkerIcon("customIcon", bI.bitmapData);
var marker:MapMarker = new MapMarker(Name);
if (NativeMaps.service.getCustomIconById("myCustomIcon")==null){
var bmp:Bitmap = new CustomMarker() as Bitmap;
var icon:CustomMarkerIcon = new CustomMarkerIcon("myCustomIcon",bmp.bitmapData);
NativeMaps.service.addCustomMarkerIcon(icon);
}
//marker.customIconId = "customIcon";
marker.title = Name;
//if(Capabilities.version.indexOf("AND")!=-1){
//marker.colour = MapMarkerColour.AND_BLUE;
//}else{
//marker.colour = MapMarkerColour.IOS_RED;
//}
marker.customIconId="myCustomIcon";
marker.setPosition(new LatLng(lat,lon));
marker.draggable = false;
marker.animatesDrop = true;
marker.infoWindowEnabled = true;
marker.showInfoWindowButton=true;
NativeMaps.service.addMarker(marker);
}
protected function centerMap(lat:Number,lon:Number):void{
NativeMaps.service.setCentre(new LatLng(lat,lon),17,true,500);
}
protected function back(event:MouseEvent):void
{
NativeMaps.service.destroyMap();
navigator.popView();
}
protected function setMapType(mapType:int):void{
NativeMaps.service.setMapType(mapType);
}
protected function calloutCreated(event:FlexEvent):void
{
this.setChildIndex(CalloutButton(event.target), this.numChildren-1);
}
]]>
</fx:Script>
<s:navigationContent>
<s:Button label="back" click="back(event)" />
</s:navigationContent>
<s:actionContent>
<s:CalloutButton id="cB" icon="@Embed('i/layers.png')" verticalPosition="before" horizontalPosition="middle" creationComplete="calloutCreated(event)">
<s:Rect width="100%" height="100%">
<s:fill>
<s:SolidColor color="#000000"/>
</s:fill>
</s:Rect>
<s:VGroup width="100%">
<s:Button label="Normal" click="setMapType(1)"/>
<s:Button label="Satellite" click="setMapType(2)"/>
<s:Button label="Terrain" click="setMapType(3)"/>
<s:Button label="Hybrid" click="setMapType(4)"/>
</s:VGroup>
</s:CalloutButton>
</s:actionContent>
-
Cool, no problem Jay. Happy to help!
-
Jay Harris commented
You're right, that resolved the issue. I moved the .init to the application load event. Thanks for the quick assistance.
-
Hi Jay
It looks like you might be calling the NativeMaps.init() method multiple times (i.e. when your view is recreated)
This method should only be called once, otherwise it will throw an error.
Can you move the NativeMaps.init() call somewhere else, like at the startup point of your application so that it's only ever called a single time?
Thanks
-
Jay Harris commented
Sorry, forgot to post the question at the top.
On the view with my map, it initially loads without a problem however, I destroyMap() before popView(). Then when returning to the view with the map it does not reinitialize. Any ideas?