1

Iam trying to find the polygon in which the given cordinate resides.

Here is the code for parsing the kml file :

for (var i = 0; i < doc[0].placemarks.length; i++) 
            {
                for(var j= 0; j<doc[0].placemarks[i].Polygon[0].outerBoundaryIs[0].coordinates.length; j++)
                {
                    var poly_lat = doc[0].placemarks[i].Polygon[0].outerBoundaryIs[0].coordinates[j].lat;
                    var poly_lon = doc[0].placemarks[i].Polygon[0].outerBoundaryIs[0].coordinates[j].lng;
                    arr_cord[j] = new google.maps.LatLng(poly_lat,poly_lon);
                    //arr_cord[i] = poly_lat + "," + poly_lon;

                //;
                }
                //alert(arr_cord);
                arr_polyoptions[i] = { path: arr_cord,strokeColor: "#FF0000",strokeOpacity: 0.8,strokeWeight: 2,fillColor: "#0000FF",fillOpacity: 0.6 };
                //polygons[i] = new google.maps.Polygon(cord_polyOptions);
                styles[i] = doc[0].placemarks[i].styleID;
                //alert(doc[0].placemarks.polygon.outerBoundaryis.LinearRing.coordinates);
            }

Now for checking through the polygons I use :

for(var curr = 0;curr<arr_polyoptions.length;curr++)
            {
                //var curr_polygon = polygons[curr];
                console.log(arr_polyoptions[curr]);
                var curr_polygon = new google.maps.Polygon(arr_polyoptions[curr]);
                //var isWithinPolygon = curr_polygon.containsLatLng(place.geometry.location);
                var isWithinPolygon = curr_polygon.containsLatLng(place.geometry.location);
                alert("isWithinPolygon = " + isWithinPolygon);
                if(isWithinPolygon == 'true')
                {
                    alert(styles[curr]);
                    break;
                }
            }

But the code produces the error Object # has no method 'containsLatLng' on the line var isWithinPolygon = curr_polygon.containsLatLng(place.geometry.location);

Is there anyone out there who can help me in this problem? Thank you in advance

On logging the curr_polygon object,the result in console is

ui
fillColor: "#0000FF"
fillOpacity: 0.6
gm_accessors_: Object
gm_bindings_: Object
latLngs: hg
b: Array[1]
0: hg
b: Array[2418]
[0 … 99]
0: N
jb: 41.8019457978553
kb: -87.62588976266039

if it helps

Arun Unnikrishnan
  • 2,339
  • 2
  • 25
  • 39
  • Have you tried inspecting the content in Chrome console ? You should be able to see `curr_polygon` keys – Jacopofar Jun 25 '13 at 13:40
  • ui fillColor: "#0000FF" fillOpacity: 0.6 gm_accessors_: Object gm_bindings_: Object latLngs: hg b: Array[1] 0: hg b: Array[2418] [0 … 99] 0: N jb: 41.8019457978553 kb: -87.62588976266039 – Arun Unnikrishnan Jun 25 '13 at 13:47

2 Answers2

2

The method name for a polygon is containsLocation, not containsLatLng.

geometry library

 containsLocation(point:LatLng, polygon:Polygon) | boolean | Computes whether the given point lies inside the specified polygon.

You didn't post your code to include the API, but you do also need to load the geometry library.

Example using geoxml3 and the geometry library

geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • I changed the checking to var isWithinPolygon = containsLocation(place.geometry.location,curr_polygon); But shows error containsLocation is not defined.BTW this is the gogole map decalration - – Arun Unnikrishnan Jun 25 '13 at 14:20
  • 1
    The full method is google.maps.geometry.poly.containsLocation() ([example](http://geocodezip.com/v3_simpleMap_containslocation.html)). If that doesn't solve the problem, please post more complete code or a jsfiddle that exhibits the problem. – geocodezip Jun 25 '13 at 14:59
  • That corrects the error.But Iam searching for the location in the city itself. But returns false for all the polygons in the kml file. If the parameters I have given for the function is not correct,will there be any error messages or simply return false? – Arun Unnikrishnan Jun 25 '13 at 15:25
  • For an answer to that you will need to provide more data: 1. a polygon; 2. a location that is in that polygon for which containsLocation returns false. – geocodezip Jun 25 '13 at 15:40
  • Can you please provide your email id or personal contact id or something so that I can directly chat with you? @geocodezip – Arun Unnikrishnan Jun 25 '13 at 15:50
  • No. I don't do this via chat or email. – geocodezip Jun 25 '13 at 16:45
0

In the Google maps v3 we have another method. You have to use containsLocation instead of containsLatLng. Here is the best example.

https://developers.google.com/maps/documentation/javascript/examples/poly-containsLocation

Parsa
  • 21
  • 5