检查点是否在多边形(地图) [英] Check if a point is in polygon (maps)
问题描述
我试图检查一个点是否在多边形中。
此刻,我尝试使用这个函数
pointInPolygon:function(point,polygon){
var i;
var j = polygon.length-1;
var inPoly = false;
var lon = point.longitude;
var lat = point.latitude; (polygon [i] [0]< lon&& polygon [j] [0])的
为(i = 0; i< polygon.length; i ++) > = lon || polygon [j] [0]< lon&&& polygon [i] [0]> = lon){
if(polygon [i] [0] +(lon- (多边形[i] [0])/(多边形[j] [0] - 多边形[i] [0])*(多边形[j] [1] b $ b inPoly =!inPoly;
}
}
j = i;
}
返回inPoly;
...此函数似乎适用于简单多边形( http://jsfiddle.net/zTmr7/3/ )但它不适用于我... $
多边形:数组[14]
数组[2]
0:-120.190625
1:29.6614549946937
数组[2]
0:-116.87275390625
1:32.6320990313992
数组[2]
0:-116.60908203125
1:34.0363970332393
数组[2]
0:-120.89375
1:41.9203747676428
Array [2]
0:-114.74140625
1:45.784484644005
数组[2]
0:-115.971875
1: 48.6489780115889
Array [2]
0:-132.758984375
1:59.9891712248332
Array [2]
0:-162.5099609375
1:68.919753529737
数组[2]
0:-168.6623046875
1:68.9828872543805
数组[2]
0:-168.4865234375
1:64.2551601036027
数组[2]
0: - 179.874356794357
1:51.0915874974707
数组[2]
0:-179.999916362762
1:13.1823178795562
数组[2]
0:-143.8771484375
1:19.9962034117847
数组[2]
0:-120.190625
1:29.6614549946937
也许你可以帮忙...提前致谢
PS。解决方案必须特别针对Bing地图或通用解决方案... Google地图API尚未提供检查方法点在多边形中。经过一番研究,我偶然发现了Ray-casting算法,它将确定X-Y坐标是否在绘制形状内。这将转化为经度和纬度。以下内容扩展了google.maps.polygon.prototype以使用此算法。在google.maps加载完成后,只需在代码中的某个位置添加以下代码:
google.maps.Polygon.prototype.Contains = function(point){
var crossings = 0,path = this.getPath();
//对于每条边
for(var i = 0; i< path.getLength(); i ++){
var a = path.getAt(i),
j = i + 1;
if(j> = path.getLength()){
j = 0;
}
var b = path.getAt(j);
if(rayCrossesSegment(point,a,b)){
crossingings ++;
}
}
//奇数个过路点?
return(crossings%2 == 1);
函数rayCrossesSegment(point,a,b){
var px = point.lng(),
py = point.lat(),
ax = a .lng(),
ay = a.lat(),
bx = b.lng(),
by = b.lat();
if(ay> by){
ax = b.lng();
ay = b.lat();
bx = a.lng();
by = a.lat();
}
//改变经度以迎合180度交叉点
if(px <0){px + = 360};
if(ax< 0){ax + = 360};
if(bx <0){bx + = 360};
if(py == ay || py == by)py + = 0.00000001; ((py> by || py< ay)||(px> Math.max(ax,bx)))返回false;
if(px
var red =(ax!= bx)? ((by-ay)/(bx-ax)):Infinity;
var blue =(ax!= px)? ((py-ay)/(px-ax)):Infinity;
返回(蓝色> =红色);
}
};
这里我们通过定义一个名为'Contains'的函数来扩展google.maps.Polygon的功能可用于确定函数参数中提供的纬度经度是否在多边形内。这里我们利用Ray-casting算法并开发了一个使用它的函数。在做了这么多的练习后,我们可以检查一个点,如下所示:
var point = new google.maps.LatLng(52.05249047600099,-0.6097412109375); var polygon = new google.maps.Polygon({path:[INSERT_PATH_ARRAY_HERE]}); if(polygon.Contains(point)){// point is inside polygon}
要获得完整的代码和演示,请转到: http://counsellingbyabhi.blogspot.in/2013/01/google-map-check- if-point-latlong.html
I'm trying to check if a point is in polygon.
At the moment I have try with this function
pointInPolygon:function (point,polygon){
var i;
var j=polygon.length-1;
var inPoly=false;
var lon = point.longitude;
var lat = point.latitude;
for (i=0; i<polygon.length; i++)
{
if (polygon[i][0]<lon && polygon[j][0]>=lon|| polygon[j][0]<lon && polygon[i][0]>=lon){
if (polygon[i][0]+(lon-polygon[i][0])/(polygon[j][0]-polygon[i][0])*(polygon[j][1]-polygon[i][1])<lat){
inPoly=!inPoly;
}
}
j=i;
}
return inPoly;
}
... this function is seems to work on simple polygon ( http://jsfiddle.net/zTmr7/3/ ) but it won't work for me... here is sample data of a polygon:
polygon: Array[14]
Array[2]
0: "-120.190625"
1: "29.6614549946937"
Array[2]
0: "-116.87275390625"
1: "32.6320990313992"
Array[2]
0: "-116.60908203125"
1: "34.0363970332393"
Array[2]
0: "-120.89375"
1: "41.9203747676428"
Array[2]
0: "-114.74140625"
1: "45.784484644005"
Array[2]
0: "-115.971875"
1: "48.6489780115889"
Array[2]
0: "-132.758984375"
1: "59.9891712248332"
Array[2]
0: "-162.5099609375"
1: "68.919753529737"
Array[2]
0: "-168.6623046875"
1: "68.9828872543805"
Array[2]
0: "-168.4865234375"
1: "64.2551601036027"
Array[2]
0: "-179.874356794357"
1: "51.0915874974707"
Array[2]
0: "-179.999916362762"
1: "13.1823178795562"
Array[2]
0: "-143.8771484375"
1: "19.9962034117847"
Array[2]
0: "-120.190625"
1: "29.6614549946937"
Maybe you can help... thanks in advance
PS. solution must be especially for Bing maps or universal solution...
The Google maps API does not already provide a method for checking points in polygons. After researching a bit I stumbled across the Ray-casting algorithm which will determine if an X-Y coordinate is inside a plotted shape. This will translate to latitude and longitude. The following extends the google.maps.polygon.prototype to use this algorithm. Simply include this code at a point in the code after google.maps has loaded:
google.maps.Polygon.prototype.Contains = function(point) {
var crossings = 0, path = this.getPath();
// for each edge
for (var i=0; i < path.getLength(); i++) {
var a = path.getAt(i),
j = i + 1;
if (j >= path.getLength()) {
j = 0;
}
var b = path.getAt(j);
if (rayCrossesSegment(point, a, b)) {
crossings++;
}
}
// odd number of crossings?
return (crossings % 2 == 1);
function rayCrossesSegment(point, a, b) {
var px = point.lng(),
py = point.lat(),
ax = a.lng(),
ay = a.lat(),
bx = b.lng(),
by = b.lat();
if (ay > by) {
ax = b.lng();
ay = b.lat();
bx = a.lng();
by = a.lat();
}
// alter longitude to cater for 180 degree crossings
if (px < 0) { px += 360 };
if (ax < 0) { ax += 360 };
if (bx < 0) { bx += 360 };
if (py == ay || py == by) py += 0.00000001;
if ((py > by || py < ay) || (px > Math.max(ax, bx))) return false;
if (px < Math.min(ax, bx)) return true;
var red = (ax != bx) ? ((by - ay) / (bx - ax)) : Infinity;
var blue = (ax != px) ? ((py - ay) / (px - ax)) : Infinity;
return (blue >= red);
}
};
Here we have extended the functionality of google.maps.Polygon by defining a function with name ‘Contains’ which can be used to determine whether the latitude longitude provided in function parameter are within the polygon or not. Here we make use of Ray-casting algorithm and developed a function using the same. After doing this much of exercise now, we can check a point as follows:
var point = new google.maps.LatLng(52.05249047600099, -0.6097412109375); var polygon = new google.maps.Polygon({path:[INSERT_PATH_ARRAY_HERE]}); if (polygon.Contains(point)) { // point is inside polygon }
For complete code and demo please go to: http://counsellingbyabhi.blogspot.in/2013/01/google-map-check-whether-point-latlong.html
这篇关于检查点是否在多边形(地图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!