Identify the Latin characters of the markers in the polygon

I have a google maps app that displays markers on load. One of the new requirements is to add a Polygon overlay that encompasses the user's selection of markers. I was able to achieve this using the Geometry Controls GMaps utility library

Now, the next step is to form a group of selected markers, for which I will need to determine if the Latin characters of the markers fall into the Latin letters of the polygon? Is there a way to determine the lat lngs of a polygon and calculate if the lat lng is within its bounds?

+2


a source to share


4 answers


Following npinti's suggestion , you can check out the following polygon point implementation for Google Maps:



0


a source


I've never come across Google Maps directly, but you can store the points that make up the polygon and then use the Point-In-polygon Algorithm to check if a given longitude and latitude point is inside the polygon or not.



+1


a source


// Create polygon method for collision detection
GPolygon.prototype.containsLatLng = function(latLng) {
    // Do simple calculation so we don't do more CPU-intensive calcs for obvious misses
    var bounds = this.getBounds();

    if(!bounds.containsLatLng(latLng)) {
        return false;
    }

    // Point in polygon algorithm found at http://msdn.microsoft.com/en-us/library/cc451895.aspx
    var numPoints = this.getVertexCount();
    var inPoly = false;
    var i;
    var j = numPoints-1;

    for(var i=0; i < numPoints; i++) { 
        var vertex1 = this.getVertex(i);
        var vertex2 = this.getVertex(j);

        if (vertex1.lng() < latLng.lng() && vertex2.lng() >= latLng.lng() || vertex2.lng() < latLng.lng() && vertex1.lng() >= latLng.lng())  {
            if (vertex1.lat() + (latLng.lng() - vertex1.lng()) / (vertex2.lng() - vertex1.lng()) * (vertex2.lat() - vertex1.lat()) < latLng.lat()) {
                inPoly = !inPoly;
            }
        }

        j = i;
    }

    return inPoly;
};

      

+1


a source


This has been updated in version 3. You can do this calculation through the google.maps.geometry.poly namespace API Documentation

0


a source







All Articles