2

我試圖限制,像這樣從谷歌地圖API地理編碼器返回的地址限制:谷歌地圖API地理編碼器,通過邊界

var geocoder = new google.maps.Geocoder(); 
var auVicSwLatLon = new google.maps.LatLng(-39.234713, 140.962526); 
var auVicNeLatLon = new google.maps.LatLng(-33.981125, 149.975296); 
var auVicLatLonBounds = new google.maps.LatLngBounds(auVicSwLatLon, auVicNeLatLon); 
geocoder.geocode(
    { 
    address: searchStr, 
    region: 'AU', 
    // bounds: auVicLatLonBounds, 
    }, 
    function(results, status) { 
    // ... do stuff here 
    } 
); 

使用限制區域的作品。然而,限制使用邊界不會 - 當我取消上面的邊界屬性的註釋時,我沒有得到任何結果。留下它的評論,我得到來自澳大利亞各地的結果。

有沒有什麼我在這裏做錯了?

謝謝!


附加信息:

相關文檔在這裏:

https://developers.google.com/maps/documentation/javascript/geocoding

這裏:

https://developers.google.com/maps/documentation/javascript/reference#Geocoder

注意,值我LatLngBounds AR已使用e爲維多利亞州(維多利亞州)。這正是我想在這裏實現的。因此,如果你知道一種替代方法來完成這個,請回答這個問題!

+0

當你使用'bounds' *而沒有'region'時會發生什麼?也許他們不能一起使用。 – 2012-08-08 08:44:37

+0

也許這是一個暫時的錯誤。我正在嘗試,並沒有奏效,但現在我得到了結果。 – 2012-08-08 08:49:46

+0

@AndrewLeach:是的,我已經嘗試過了,沒有地區的英鎊......不幸的是,無濟於事。 – bguiz 2012-08-08 10:45:03

回答

1

爲什麼不手動查找滿足邊界要求的結果?

我遍歷結果並使用lat和lng值檢查位於受限區域內的第一個結果。你也可以得到所有落在裏面的結果。

function searchFromAddress() { 
    var address = document.getElementById("txtBxAddress").value; 
    // Check if input is not empty 
    if (address.length < 1) { 
     return; 
    } 
    var geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({ 'address': address }, 
     function (results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
       var point; 

       // Find first location inside restricted area 
       for (var i = 0 ; i < results.length ; i++) { 
        point = results[i].geometry.location; 
        // I compare my lng values this way because their are negative 
        if (point.lat() > latMin && point.lat() < latMax && point.lng() < lngMin && point.lng() > lngMax) { 
         map.setCenter(point); 
         var marker = new google.maps.Marker({ 
          position: point, 
          map: map, 
          title: "You are here", 
          icon: home_pin 
         }); 
         break; 
        } 
        // No results inside our area 
        if (i == (results.length - 1)) { 
         alert("Try again"); 
        } 
       } 
      } 
     }); 
}