地理位置HTML5 getCurrentLocation回调

地理位置HTML5 getCurrentLocation回调,html,geolocation,Html,Geolocation,如何将回调函数添加到将coords或position作为args传递给全局变量的getCurrentLocation方法?。允许该对象将坐标传递给getCurrentLocation外部的全局变量 //Global scope var Location= {coords:null; } function getLocation(position) { position.coords.latitude; Location.coords=coords.clone(); } somewh

如何将回调函数添加到将coords或position作为args传递给全局变量的getCurrentLocation方法?。允许该对象将坐标传递给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 !

谢谢。

您可以阅读关于如何使用Geolocation API的所有内容,这是一个非常棒的站点。默认情况下,getCurrentLocation将回调作为参数。回调函数接收一个位置对象

navigator.geolocation.getCurrentLocation(function(pos){
  console.log("I'm located at ",pos.coords.latitude,' and ',pos.coords.longitude);
});
建议也使用Paul Irish的shim作为旧浏览器的备用工具:

也应该可以帮助您解决问题。

在您的代码中,这一行:

Location.coords=coords.clone();
使用未声明的变量
coords
。函数的参数与您调用的坐标
位置
。这是打字错误吗?

不应该是:

getCurrentPosition