我试图找到给定坐标所在的多边形。

这是解析kml文件的代码:

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


现在通过多边形检查:

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


但是代码会产生错误Object#,在var isWithinPolygon = curr_polygon.containsLatLng(place.geometry.location)行上没有方法'containsLatLng';

有没有人可以帮助我解决这个问题?先感谢您

记录curr_polygon对象时,控制台中的结果为

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


如果有帮助

最佳答案

多边形的方法名称是containsLocation,而不是containsLatLng。

geometry library

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


您没有发布包含API的代码,但是还需要加载几何库。

Example using geoxml3 and the geometry library

08-06 21:14