Reputation: 5651
I'm trying to write function that returns the geolocation using html5. However my function never returns. How come?
function getCurLocation() {
navigator.geolocation.getCurrentPosition(function(position) {
return [position.coords.latitude, position.coords.longitude];
});
}
Upvotes: 0
Views: 151
Reputation: 191749
navigator.geolocation.getCurrentPosition
is an asynchronous call. As with all Ajax, you can't return it's value synchronously with the rest of the code. You can't know when it will complete or even if it will complete. Any work that relies on the response (position
in this case) of the ajax call must be done in the ajax callback.
Upvotes: 2