2011-06-21 74 views
0

我想爲地圖工具做一個簡單的地理編碼功能。我得到的地理編碼很好,但我希望將位置對象作爲地理編碼函數的返回值傳遞回去。從函數返回GMap位置對象

事情是這樣的:

function codeAddress(address) { 
    geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      var location = results[0].geometry.location; 
      console.dir(location); 
      return location; 
     } else { 
      return false; 
     } 
    }); 
} 

位置項目的console.dir顯示預期的位置對象,因此函數被調用併成功返回的數據。

這個函數被另一個進程調用,然後它會建立標記。

if (coordinates = codeAddress(myaddress){ 
    // do stuff 
} 

然而,coordinates變量總是評估爲不確定,所以要「做的東西」的條件,從來沒有遇到過。

我知道我可能錯過了關於座標變量var的定義,但我不確定它是什麼。

感謝您的幫助。

基本代碼在:http://jsfiddle.net/2TXZ4/3/雖然地圖沒有被繪製出於任何原因。

+0

好吧,它看起來像我這樣的問題是,這是一個異步請求 - 如果我在codeAddress函數中硬編碼返回值,它都按預期工作。所以它必須是請求的性質。但仍然不確定如何最好地處理這個問題。 – julio

回答

0

geocode方法是asynchonous,所以你不需要做任何處理您提供給該方法的回調,也可以提供一個回調到您codeAddress方法類似的這樣的問題/答案描述(How do I return a variable from Google Maps JavaScript geocoder callback?)。在下面的代碼中,我將doStuff函數中的功能轉移到了geocode函數的回調中 - 它應該可以工作,但我無法使用您提供的jfiddle鏈接(可能是由於Google Maps API鍵)進行測試。我希望這有幫助!

function codeAddress(address) { 
    geocoder.geocode({ 
     'address': address 
    }, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      var location = results[0].geometry.location; 
      console.log(location); 
      map.setCenter(location); 
      var marker = new google.maps.Marker({ 
       map: map, 
       position: location 
      }); 
     } else { 
      alert("Geocode was not successful for " + address); 
     } 
    }); 
} 


window.doStuff = function() { 
    var address = document.getElementById("address").value; 
    codeAddress(address); 
} 
+0

這是正確的,我最終做了什麼。我仍然在「doStuff」函數中創建了標記,但是隨後在codeAddress回調中使用了marker.setPosition(location)方法來設置位置。 – julio

0

if (coordinates = codeAddress(myaddress) - 這是一項任務,而不是比較。

好吧,這裏是一個瘋狂的黑客(即不工作):

<script type="text/javascript"> var geocoder; var loc; var l = false; 
function initialize() { 

geocoder = new google.maps.Geocoder(); 

var mapDiv = document.getElementById('map_canvas'); 
    map = new google.maps.Map(mapDiv, { 
     center: new google.maps.LatLng(-34.397, 150.644), 
     zoom: 8, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    });  } 


function codeAddress(address) { 
geocoder.geocode({ 
    'address': address 
}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     loc = results[0].geometry.location;    l = true; 
     console.log(loc);   //alert (loc); 
     //return loc;  doStuff(loc) 
    } else { 
     return false; 
    } 
}); } 


function doStuff(geo_marker){ if(l==false){ 
var thisaddress = document.getElementById("address").value; 
var myResult = codeAddress(thisaddress); }else{ 

if (loc) { 
    map.setCenter(loc); 
    var marker = new google.maps.Marker({ 
     map: map, 
     position: loc 
    }); 
l = false; //important to use function more than once :)) 
} else { 
    alert("Geocode was not successful"); 
} } 

}; 

google.maps.event.addDomListener(window, 'load', initialize); 
</script> 



<body> 
<div> 
<input id="address" type="textbox" value="Sydney, NSW"> 
<input type="button" value="Geocode" onclick="window.doStuff()"> 
</div> 
<div id="map_canvas" style="height:600px; width: 500px; position: absolute; 
    top: 80px"> </div> 

</body> 

我認爲它更有意義做地理編碼和添加標記在一個函數映射不過,爲了避免這種回調和功能之間。

+0

@ AR--我也嘗試了以下方法,結果相同(ccord未定義): 'var coord = codeAddress(tempaddress); \t \t \t \t console.dir(coord); \t \t \t \t if(coord){// dostuff }' – julio

+0

我已更新我的答案。這個例子絕對有效,你只需要在你認爲合適的時候更新它。 –