开发者

GeoLocation HTML5 getCurrentLocation callback

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 th开发者_StackOverflowe 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.


Shoudn't be :

getCurrentPosition


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


Finding your position with Geolocation | HTML5 Doctor should also help you out.


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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜