2014-09-24 20 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

相關問題