Reputation: 1433
How to add a callback function to getCurrentLocation method passing coords or position as args to a global variable?. The object is allow to pass the coord to a global variable outside getCurrentLocation.
//Global scope
var Location= {coords:null;
}
function getLocation(position)
{
position.coords.latitude;
Location.coords=coords.clone();
}
somewhere
navigator.geolocation.getCurrentLocation(getLocation, error)
Location.coords is NULL !
thanks.
Upvotes: 0
Views: 2605
Reputation: 44064
You can read all about how to use the geoloction API at http://diveintohtml5.ep.io/geolocation.html which is a fantastic site. By default getCurrentLocation takes a callback as an argument. The callback receives a position object.
Upvotes: 0
Reputation: 13620
In your code this row:
Location.coords=coords.clone();
uses a variable coords
that isn't declared. The parameter to the function with the coordinate you have called position
. Is it a typo?
Upvotes: 0
Reputation: 18870
Finding your position with Geolocation | HTML5 Doctor should also help you out.
Upvotes: 0
Reputation: 846
navigator.geolocation.getCurrentLocation(function(pos){
console.log("I'm located at ",pos.coords.latitude,' and ',pos.coords.longitude);
});
Suggest using Paul Irish's shim as a fallback for older browsers too: https://gist.github.com/366184
Upvotes: 0