2014-09-24 40 views
1

假设我有以下的纬度/经度,在地图上创建区域的多边形Android的检查,如果我的纬度/经度位置withing给定区域

39.888932, -95.557237 
42.156511, -101.347921 
40.322707, -101.040304 
38.299884, -100.447042 
36.731433, -96.623800 

现在,我怎么检查是否40.356203, -97.304952这个纬度/经度为在该给定区域内还是没有。

我该怎么做?

+0

快速正脏边框算法可以工作的论文,但同时也容易实现和理解。 (假设你可以容忍假阳性) – Machinarius 2014-09-24 13:16:28

+0

http://stackoverflow.com/questions/26014312/identify-when-current-location-is-in-the-polygon-and-do-some-taskthere 如果这是你的要求,ans在下面发布。 – Supriya 2014-09-24 13:19:21

回答

4

刚刚尝试识别多边形中的点的Ray Casting算法。这工作完美。

参考http://en.wikipedia.org/wiki/Point_in_polygon的光线投射

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

在isPointInPolygon方法中添加您的参数。 参数是1.您的位置。 2.set位置(多边形的) – Supriya 2014-09-24 13:18:01

+0

它的工作表示感谢 – user2729183 2014-09-24 13:30:54

相关问题