2014-10-08 30 views
-1

即時通訊使用地理編碼器js對象試圖尋找最接近latlng的地址co-ord。例如,我將愛爾蘭的一個地址傳遞給latlay房地產,它應該優先於另一個國家(或縣內等)返回愛爾蘭地址,但它返回西班牙語的位置...谷歌地理編碼地址最接近latlng或邊界

任何人都可以幫忙這個?

function codeAddress() { 
    var address = document.getElementById('address').value; 
    var latlng = new google.maps.LatLng(53.2734,-7.778320310000026); 
    geocoder.geocode({ 
     'address': address, 
     'latLng': latlng 
    }, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     map.setCenter(results[0].geometry.location); 


     map.setZoom(15); 
     marker = new google.maps.Marker({ 
      map: map, 
      position: results[0].geometry.location, 
      draggable:true 
     }); 
    } else { 
     alert('Geocode was not successful for the following reason: ' + status); 
    } 

    document.getElementById('lat').value = marker.position.lat(); 
    document.getElementById('long').value = marker.position.lng(); 

    google.maps.event.addListener(marker, 'dragend', function() { 
     document.getElementById('lat').value = marker.position.lat(); 
     document.getElementById('long').value = marker.position.lng(); 
    } 
    ); 
}); 

回答

1

對於請求沒有latLng -property。

要搜索結果限制於特定國家使用componentRestrictions

{ 
    'address': address, 
    componentRestrictions:{country:'ie'} 
} 
+0

謝謝您的回答! Latlng在這裏的文檔https://developers.google.com/maps/documentation/javascript/geocoding#GeocodingRequests,這是錯誤的文檔?將結果限制在國家可以改善事情,但在同一個國家可能有兩個相同的地址。我最終讓用戶進入一個城鎮,然後他們可以輸入地址來解決這個問題,但我希望能夠像邊界一樣。 – David 2014-10-08 23:08:07

+0

這部分文檔是錯誤的,在參考文獻中沒有'latLng'(屬性是'location',參見https://developers.google.com/maps/documentation/javascript/reference#GeocoderRequest)。但沒關係,你只能使用其中的一個,位置或地址。當你想要時,'bounds'也可以使用。 – 2014-10-08 23:17:16

+0

有一個latLng屬性,它用於反向地理編碼。您不能同時使用地址和latLng。 [來自文檔](https://developers.google.com/maps/documentation/javascript/geocoding#GeocodingRequests):這些字段在下面解釋。 address(必填*) - 您想要進行地理編碼的地址。 latLng(必需*) - 您希望獲得最近的人類可讀地址的LatLng。 *注意:您可以傳遞地址或latLng來查找。 (如果你傳遞latLng,地理編碼器會執行所謂的反向地理編碼,請參閱反向地理編碼以獲取更多信息。) – geocodezip 2014-10-08 23:41:32