0
votes

I have a working Google Maps API script that has 7 polygons via the google.maps.Polygon "paths" feature.

For example:

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

This allows you to submit an address and do a geocoder containslocation to see if you are in one of my polygons

Works perfect except when it returns it does not tell me in which polygon path it found the address.

Search as I might, I cannot find any examples of code showing how to do this. I assume there must be some over looked tag that returns the paths name, but again, no luck.

Here is the code as it works today:

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

I basically check and then return an HTML result to the user with my updateHTML(); function. There are a few other variables that come from elsewhere, but this is the jist of the query.

Any ideas on how to perform the same actions but end knowing which polygon path I came up with (if any)?

1

1 Answers

1
votes

You actually only have one polygon, with multiple paths.

Simplest way to solve your problem is to make separate polygons for each path. Then call containsLocation on each of those polygons, the one that returns true is the one that contains the address.