我坚持这样的问题:我想检索根据地理位置,以便使用它在我的calcRoute功能作为起点,例如定义的位置,这里是代码:如何检索本地变量(地理位置HTML5和Google Maps API)?
function calcRoute(){
var pos;
navigator.geolocation.getCurrentPosition
(
function(position)
{
pos = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
}
);
var start = pos;
alert(start); // pb here : start is undefined, how could I get the pos value ?
//...
}
非常感谢您的帮助!
试着添加一个setTimeout,因为匿名函数是由另一个线程执行的,并且你试图在设置之前得到pos值。 – Pouki 2013-04-10 09:14:09
不,请尝试了解异步调用和使用回调是如何工作的。希望结果可能在那时使用setTimeout是相当无稽之谈。 – CBroe 2013-04-10 09:20:46