Help needed with getting location and angular philosophy

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??

If you need location in more than one controller, you definitely want to use the factory or “Service”. This is nice as you don’t need to copy the same code over and over.

Look at this sample here of getting geolocation. It’s doing it in the controller though.

I’ve modified it a bit to show using a service to get the Lat & Long. Click the “Find Me” link at the bottom left to get your position. Be sure to notice that the browser will ask you for your location right under the URL bar.

Thanks very much for example. I need promise :wink:

Hi Calendee,

When the browser asks for permissions, I am seeing a long url on title of thet popup which is not looking good like “localhost/akjhaskhdsad2781631/index.html would like to access your location”.

Is there a way to change this? and make it like “Application would like to access your location”

Hi Vamshi,

I am having the same problem; were you ever able to find a solution for this?
Thanks

hii, i am new with ionic, i use this code is run correctly on my laptop but after build it is not display my current location in mobile. what can i do help me.

HI @cagan327

+1 similar issue