谷歌地图API,containslocation()哪个多边形是你的地址?

时间:2017-04-19 20:11:18

标签: javascript php google-maps google-maps-api-3

我有一个有效的Google Maps API脚本,它通过google.maps.Polygon"路径"有7个多边形。特征

例如:

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);

这允许您提交地址并执行地理编码器包含位置以查看您是否在我的多边形之一

完美无缺,除非它返回时不会告诉我它在哪个多边形路径中找到了地址。

尽可能搜索,我找不到任何显示如何执行此操作的代码示例。我假设必须有一些看过去的标签返回路径名称,但同样,没有运气。

以下是今天的代码:

// 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>";
    }
}

我基本上检查然后使用我的updateHTML()将HTML结果返回给用户;功能。还有一些其他变量来自其他地方,但这是查询的要点。

关于如何执行相同操作但是知道我想出哪个多边形路径(如果有)的任何想法?

1 个答案:

答案 0 :(得分:0)

实际上只有一个多边形,有多个路径。

解决问题的最简单方法是为每个路径制作单独的多边形。然后在每个多边形上调用containsLocation,返回true的那个是包含地址的那个。

相关问题