I’m new to angularJS. Wnat to make app with user’s current location.
This location will be used in several view with different controlers.
So I make a factory function:
znajdzPozycje: function() {
navigator.geolocation.getCurrentPosition(function(pos) {
geoLokalizacja.x = pos.coords.latitude;
geoLokalizacja.y = pos.coords.longitude;
console.log(geoLokalizacja);
}, function(error) {
alert('Unable to get location: ' + error.message);
});
},
getGeoLokalizacja: function() {
if (geoLokalizacja.x && geoLokalizacja.y)
return geoLokalizacja;
},
which I execute in AppControler.
At start of App in indexCtrl I want to add marker with current position on map:
var geoLokalizacja = App.getGeoLokalizacja();
if (geoLokalizacja) {
$scope.mapa.marker = {
latitude: geoLokalizacja.x,
longitude: geoLokalizacja.y,
};
}
App doesn’t have this position at start and I don’t know how to use it from factory when it will be available?
Should I get position (navigator.geolocation.getCurrentPosition) in every controller when I want to use current position? Or can I use factory for that? How to change controller when position is available in factory??