2017-04-19 72 views
-1

我有一個可以通過google.maps.Polygon「路徑」功能使用7個多邊形的Google Maps API腳本。Google Maps API,containslocation()哪個多邊形是您的地址?

例如:

var gigaMap = new google.maps.Polygon({ 
    paths: "britt,shallow,cherokee,clover,freyer,sandy,wyandot", 
    strokeColor: "#990000", 
    strokeOpacity: 0.9, 
    strokeWeight: 2, 
    fillColor: "#990000", 
    fillOpacity: 0.2 
}); 

// apply the polygon 
gigaMap.setMap(map); 

這允許你提交一個地址做地理編碼containslocation看到,如果你是我的一個多邊形

作品完美的,當它返回它並沒有告訴除我在哪個多邊形路徑中找到了地址。

搜索我可能,我找不到任何示例代碼顯示如何做到這一點。我假設必須有一些超過標籤的返回路徑名稱,但是再次,沒有運氣。

下面是代碼,因爲它的工作原理今天:

// receive address, geocode and point map to location 
function geocodeAddress(geocoder, resultsMap) { 

geocoder.geocode({"address": formaddress}, function(results, status) { 
    if (status === "OK") { //this verifies that Google was able to convert the address 
     if (results[0].geometry.location_type == "ROOFTOP"){ //this verifies that Google found an actual address 

      // Is our address within a polygon defined? (true/false) 
      myresult = google.maps.geometry.poly.containsLocation(results[0].geometry.location, gigaMap); 
      updateHTML(myresult,formphase,formlocation); 

      // centers map on address location 
      resultsMap.setCenter(results[0].geometry.location); 

      // zoom result map in to marker and swap terrain 
      resultsMap.setZoom(17); 
      resultsMap.setMapTypeId("satellite"); 

      // place the marker on map 
      var marker = new google.maps.Marker({ 
       map: resultsMap, 
       title: results[0].formatted_address, 
       position: results[0].geometry.location 
      }); 
     } else { 
     document.getElementById("address_result").innerHTML = "<span class='title'>Hum... we are unable to locate the address you entered.</span><br>If you feel your address should have matched a valid Google location, we encourage you to <a href='/#contact'>contact us</a> or <a href='/#service'>search again</a>."; 
      } 
     } else { 
      document.getElementById("address_result").innerHTML = "<span class='title'>Oops!, we are unable to access the Google Geocoder service at this time.</span>"; 
     } 
    }); 
} 

function updateHTML(status,phase,location){ 
    if (status === true && phase == "signup"){ 
     document.getElementById("address_result").innerHTML = "<span class='title'>Congratulations, your address is located within a Service Area!</span>"; 
    }else if (status === true && phase == "survey"){ 
     document.getElementById("address_result").innerHTML = "<span class='title'>Congratulations, your address is located within a prospective Service Area!</span>"; 
    }else if(status === false){ 
     document.getElementById("address_result").innerHTML = "<span class='title'>Hum... at this time your address is not located within a Service Area!</span>"; 
    }else{ 
     document.getElementById("address_result").innerHTML = "<span class='title'>Oops!, we are unable to access the Google Polygon service at this time.</span>"; 
    } 
} 

我基本檢查,然後一個HTML結果返回到與我updateHTML用戶();功能。還有其他一些變量來自其他地方,但這是查詢的焦點。

關於如何執行相同操作但結束瞭解我想出哪個多邊形路徑(如果有)的任何想法?

+0

請提供證明問題的[mcve]。 – geocodezip

回答

0

您實際上只有一個多邊形,具有多個路徑。

解決您的問題最簡單的方法是爲每個路徑製作單獨的多邊形。然後在每個這些多邊形上調用containsLocation,返回true的那個是包含地址的那個。

+0

謝謝,我從來沒有想到(如你所說),我只有科技有一個單一的聚合與7路徑。我想我正在接近它,因爲「路徑」只是讓它在一張地圖上繪製7個多邊形。 –

相關問題