2013-04-17 125 views
1

我正在嘗試使用地理位置進行一些操作。我想要返回一個變量的地理位置緯度和經度作爲數組。它不起作用。我認爲代碼中的第一行是問題。是否有可能得到這樣的價值(與回報)?Javascript地理位置

startingPoint = navigator.geolocation.getCurrentPosition(onGeoSuccess); 

function onGeoSuccess(position) { 
    start['lat'] = position.coords.latitude; 
    start['lng'] = position.coords.longitude; 
    return start; 
} 

有沒有更好的解決方案?我不想在onGeoSuccess中執行其他代碼,我想返回值。

回答

1

代碼,你可以嘗試這樣的事情:

function geolocate() { 
    if (navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(function(position) { 
     var geolocation = new google.maps.LatLng(
     position.coords.latitude, position.coords.longitude); 
     var circle = new google.maps.Circle({ 
     center: geolocation, 
     radius: position.coords.accuracy 
     }); 
     autocomplete.setBounds(circle.getBounds()); 
    }); 
    } 
} 
0

執行 - 檢查 - 如果地理位置被支持或不,做這樣的

function getLocation() 
    { 
    if (navigator.geolocation)//check for support 
    { 
    navigator.geolocation.getCurrentPosition(showPosition); 
    } 
    else{x.innerHTML="Geolocation is not supported by this browser.";} 
    } 
function showPosition(position) 
    { 
    x.innerHTML="Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude; 
    } 
+0

謝謝。我知道。但我的問題是,我可以在代碼中的第5行上做這樣的事情:mypos = navigator.geolocation.getCurrentPostion(showPosiiton); ? – FishWave