GeoLocation HTML5 getCurrentLocation回调

时间:2011-07-16 15:38:26

标签: html5 geolocation

如何为getCurrentLocation方法添加回调函数,将coords或args定位为全局变量?该对象允许将coord传递给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 !

感谢。

5 个答案:

答案 0 :(得分:1)

不应该:

getCurrentPosition

答案 1 :(得分:0)

您可以在http://diveintohtml5.ep.io/geolocation.html这个非常棒的网站上阅读有关如何使用地理位置API的所有内容。默认情况下,getCurrentLocation将回调作为参数。回调接收位置对象。

答案 2 :(得分:0)

navigator.geolocation.getCurrentLocation(function(pos){
  console.log("I'm located at ",pos.coords.latitude,' and ',pos.coords.longitude);
});

建议使用Paul Irish的垫片作为旧版浏览器的后备:https://gist.github.com/366184

答案 3 :(得分:0)

答案 4 :(得分:0)

在你的代码中这一行:

Location.coords=coords.clone();

使用未声明的变量coords。具有您称为position的坐标的函数的参数。这是一个错字吗?