Titanium.Geolocation.getCurrentPosition won't work within a function
I must be misunderstanding something fundamental. Calling Titanium.Geolocation.getCurrentPosition with code right on a page works. But, putting the same code in a function/event handler fails.
Sample code on pastie http://pastie.org/1113838
The first chunk of code outputs the location data as expected. The second block attaches the same code to a button. When I click the button and I get 'undefined' values.
So, what am I doing wrong? Or, what am I misunderstanding?
My reason for putting the code in a handler (goal is actually a function called by the handler) is so that I determine the location & take action only when someone clicks the button. I figure that's nicer to battery life in case they leave the app open while walking around – the phone won't be constantly determining its location.
6 Answers
-
I believe the getCurrentPosition function it's async, so you will need to put the logging function inside it. not outside.
else { if (e.coords.accuracy <= 500) { mylongitude = e.coords.longitude; mylatitude = e.coords.latitude; Titanium.Geolocation.reverseGeocoder(mylatitude, mylongitude, function(evt){ var places = evt.places; entry_address = places[0].address; }); Titanium.API.info('Event handler: ' + mylongitude + ' (long) ' + mylatitude + ' (lat) ') } } });
-
Can you post your part of the code that is not working? May be some variables scope issue
-
Ok
I did not test it, but what I saw at a first glance is that you are trying to return the values on an async function ( which doesn't work ).You are doing something like this
var retValue = getMyLocation()
What really happens is this:
the getMyLocation() function starts, initiate the Titanium.Geolocation.getCurrentPosition() function async then exists.
the Titanium.Geolocation.getCurrentPosition function starts the gps, do it's stuf and finishes a lot later than your function closes. That means to be async.
You will have to call all your location dependent function like thealert(typeof(retValue));
when Titanium.Geolocation.getCurrentPosition finishes, so instead of
return theLocation;
and of course to handle the error case too.
Actually you are doing it very well in the button event where you triggering the openSaleWindow.
Now if you cannot wait you could "fool" a little the user, starting the geolocation exactly whan the app opens and cache it, being async you can go on with the rest of the interface, and when u have the location data use openSaleWindow
If you need more time, use a activity indicator.
Let me know if it makes sense.
-
Well you can do this for sure. But I think it's against the user's expectations to need to refresh the location by himself.
If you want to do this(get the location by request ),the best way is to let the user his location is updated with an activity indicator, that it's hidden when the function() the async one ends and you open or refresh the saleswindow. -
Hi Tim,
I am experiencing the same issue. Did you get to a solution here?
João
-
duplicate