In my Ionic Framework app I use this state:
.state('tab.map', {
cache: false,
url: '/map',
params: {'location': null},
views: {
'tab-map': {
templateUrl: 'templates/tab-map.html',
controller: 'MapCtrl'
}
}
})
in template:
<div id="map" data-tap-disabled="true"></div>
in controller:
if ($stateParams['location']) {
var location = Locations.get($stateParams['location']);
Map.putMarker(location);
} else {
Map.putMarker();
}
service:
.factory('Map', function (Locations, Logs) {
var map;
var defaultLocation = {latitude: 46.42, longitude: 30.72, accuracy: 1000};
var point; // current position
return {
latLong: function (location) {
location = location || Locations.last() || defaultLocation;
return new google.maps.LatLng(location.latitude, location.longitude);
},
destroy: function () {
map = null;
},
get: function () {
if (!map) {
var options = {
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP,
zoomControl: false,
center: this.latLong()
};
map = new google.maps.Map(document.getElementById("map"), options);
}
return map;
},
putMarker: function (location) {
location = location || Locations.last();
var latLong = this.latLong(location);
var path = isNaN(location.heading)
? google.maps.SymbolPath.CIRCLE
: google.maps.SymbolPath.FORWARD_CLOSED_ARROW;
var color = '#AA0000';
var params = {
position: latLong,
icon: {
path: path,
rotation: parseInt(location.heading),
strokeWeight: 3,
fillColor: color,
strokeColor: color,
scale: 5
},
map: this.get()
};
if (point) point.setMap(null);
point = new google.maps.Marker(params);
// calc bounds for fitting to the map
if (this.navigate) {
var circle = new google.maps.Circle({
center: latLong,
radius: parseInt(location.accuracy)
});
this.get().panToBounds(circle.getBounds());
}
}
}
})
How I can reuse one instance of Google Maps for different states?
ui-sref="tab.map({location: location.id})"
Without caching it makes for each state new ion-view tag block - in this case I need unique ID for map DIV...
With state caching I'm trying to re-initializing Map object - but something goes wrong:(
It may have a better solution? May be have a NICE solution to pass parameters in controller w/o changing the state?