我试图找到给定坐标所在的多边形。
这是解析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 # has no method 'containsLatLng' on line var isWithinPolygon = curr_polygon.containsLatLng(place.geometry.location);
有没有人可以帮助我解决这个问题?先感谢您
在记录 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
如果有帮助