2011-07-16 38 views
0

如何添加回调函数getCurrentLocation方法将坐标或位置作为参数传递给全局变量?该对象允许将坐标传递给getCurrentLocation之外的全局变量。地理位置HTML5 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 ! 

谢谢。

回答

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

建议使用保罗爱尔兰的垫片为旧浏览器也回退:https://gist.github.com/366184

0

在你的代码,该行:

Location.coords=coords.clone(); 

使用可变coords未声明。该函数的参数与您调用的坐标position。这是一个错字吗?