我有一個可以通過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用戶();功能。還有其他一些變量來自其他地方,但這是查詢的焦點。
關於如何執行相同操作但結束瞭解我想出哪個多邊形路徑(如果有)的任何想法?
請提供證明問題的[mcve]。 – geocodezip