2012-03-01 76 views
2

这是我有:getCurrentPosition返回什么?

var position; 
    navigator.geolocation.getCurrentPosition(onSuccess, onError); 

    function onSuccess(pos) { 
position = { latitude: pos.coords.latitude, longitude: pos.coords.longitude} ; 
//position = { latitude:43.465099,longitude:-80.520344}; 
    } 

    function onError(error) { 
     alert('code: ' + error.code + '\n' + 
       'message: ' + error.message + '\n'); 
    } 


    var myOptions = { 
     center: new google.maps.LatLng(
     position.latitude,position.longitude 
    ), 
     zoom: 14, 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
     disableDefaultUI: true 
    }; 

当我使用注释掉线,该程序工作正常,它缩放到硬编码位置。当我使用pos.coords行时,没有任何反应。我说错了吗?我不能像这样把它放在我的变量中,还是会返回其他东西?

回答

1

你似乎是正确使用的结果,但我会有点不同编码JSON:

position = { "latitude": pos.coords.latitude, 
      "longitude": pos.coords.longitude};