本文介绍了Android的检查,如果我的纬度/经度位置withing给定区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有以下的纬度/经度创建区域的地图多边形
Suppose I have the following lat/lng that creates a polygon of area in map
39.888932, -95.557237
42.156511, -101.347921
40.322707, -101.040304
38.299884, -100.447042
36.731433, -96.623800
现在我怎么能检查 40.356203,-97.304952
这个纬度/经度是给定区域内或没有。
Now How can I check if 40.356203, -97.304952
this lat/lng is within that given area or not.
我怎样才能做到这一点?
How can I do this??
推荐答案
刚试过光线投射算法,识别多边形点。这完美的作品。
Just tried Ray Casting algorithm which identifies point in polygon. This works perfect.
http://en.wikipedia.org/wiki/Point_in_polygon 雷的论文-Casting
Refer http://en.wikipedia.org/wiki/Point_in_polygon for thesis of Ray-Casting
private boolean isPointInPolygon(LatLng tap, ArrayList<LatLng> vertices) {
int intersectCount = 0;
for (int j = 0; j < vertices.size() - 1; j++) {
if (rayCastIntersect(tap, vertices.get(j), vertices.get(j + 1))) {
intersectCount++;
}
}
return ((intersectCount % 2) == 1); // odd = inside, even = outside;
}
private boolean rayCastIntersect(LatLng tap, LatLng vertA, LatLng vertB) {
double aY = vertA.latitude;
double bY = vertB.latitude;
double aX = vertA.longitude;
double bX = vertB.longitude;
double pY = tap.latitude;
double pX = tap.longitude;
if ((aY > pY && bY > pY) || (aY < pY && bY < pY)
|| (aX < pX && bX < pX)) {
return false; // a and b can't both be above or below pt.y, and a or
// b must be east of pt.x
}
double m = (aY - bY) / (aX - bX); // Rise over run
double bee = (-aX) * m + aY; // y = mx + b
double x = (pY - bee) / m; // algebra is neat!
return x > pX;
}
这篇关于Android的检查,如果我的纬度/经度位置withing给定区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!