2016-03-20 37 views
4

osmdroidボーナスパックと一緒にアンドロイドでosmdroidを使用しています。私は多角形を描きました。今私はGeoPoint(緯度、経度)描画されたポリゴン内にあるかどうかをチェックしたいと思います。どうすればいいのですか?OSMBonusPack:GeoPointがポリゴン内にあるかどうかを確認してください

ここでは、motionEventオブジェクトのみを受け入れ、動作しないメソッドを含むcontainsメソッドを使用しています。それを行う他の方法はありますか?

私のコードは以下の通りです。

GeoPoint gPt0 = new GeoPoint(23.215210, 72.648600); 
GeoPoint gPt1 = new GeoPoint(23.216030, 72.648340); 
GeoPoint gPt2 = new GeoPoint(23.217400, 72.649070); 
GeoPoint gPt3 = new GeoPoint(23.216570, 72.650290); 
GeoPoint gPt4 = new GeoPoint(23.214750, 72.649130); 

List<GeoPoint> list = new ArrayList<>(); 
list.add(gPt0); 
list.add(gPt1); 
list.add(gPt2); 
list.add(gPt3); 
list.add(gPt4); 
Polygon polygon = new Polygon(getApplication()); 
polygon.setPoints(list); 
GeoPoint center = new GeoPoint(23.21612, 72.64933); 
map.getOverlays().add(polygon); 

MotionEvent event = MotionEvent.obtain(1, 1, MotionEvent.ACTION_HOVER_ENTER, (float)23.21602, (float) 72.64926, 1); 

Log.e("CONTAINS", polygon.contains(event)+""); 

答えて

2

新しいモーションイベントを定義すると、XとYは、画面内の位置(ピクセル)ではなく、マップ内の位置です。あなたがそのメソッド(https://github.com/osmdroid/osmdroid/blob/master/osmdroid-android/src/main/java/org/osmdroid/bonuspack/overlays/Polygon.java)のソースコードが表示される場合は、唯一のMotionEvent内座標

/** Important note: this function returns correct results only if the Polygon has been drawn before, 
* and if the MapView positioning has not changed. 
* @param event 
* @return true if the Polygon contains the event position. 
*/ 
public boolean contains(MotionEvent event) 

が使用されています:

return region.contains((int)event.getX(), (int)event.getY()); 
+0

しかし、スクリーンポイントを渡すことで、lat-longがポリゴン内にあるかどうかを知ることができます(これはlat-longから作成したものです)。 –

+0

containsは地図のクリックに使用されます。座標がポリゴンの内側にあるかどうかを知りたい場合は、このライブラリでhttps://github.com/sromku/polygon-contains-pointを試してみることができます。この回答にコメントがありますhttp://stackoverflow.com/questions/15816928/test-of-point-inside-polygon-in-android – mromer

+0

私はすでにこれを試しましたが、osmbonus pack –

2

利用したアルゴリズムの下には試してみてくださいさらにosmdroidにおけるJavaのドキュメントにおけるいくつかの考慮事項がありますJavaの

class Point 
 
{ 
 
    int x, y; 
 
    
 
    Point() 
 
    {} 
 
    
 
    Point(int p, int q) 
 
    { 
 
     x = p; 
 
     y = q; 
 
    } 
 
} 
 
    
 
public class Position_Point_WRT_Polygon 
 
{ 
 
    
 
    public static boolean onSegment(Point p, Point q, Point r) 
 
    { 
 
     if (q.x <= Math.max(p.x, r.x) && q.x >= Math.min(p.x, r.x) 
 
       && q.y <= Math.max(p.y, r.y) && q.y >= Math.min(p.y, r.y)) 
 
      return true; 
 
     return false; 
 
    } 
 
    
 
    public static int orientation(Point p, Point q, Point r) 
 
    { 
 
     int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y); 
 
    
 
     if (val == 0) 
 
      return 0; 
 
     return (val > 0) ? 1 : 2; 
 
    } 
 
    
 
    public static boolean doIntersect(Point p1, Point q1, Point p2, Point q2) 
 
    { 
 
    
 
     int o1 = orientation(p1, q1, p2); 
 
     int o2 = orientation(p1, q1, q2); 
 
     int o3 = orientation(p2, q2, p1); 
 
     int o4 = orientation(p2, q2, q1); 
 
    
 
     if (o1 != o2 && o3 != o4) 
 
      return true; 
 
    
 
     if (o1 == 0 && onSegment(p1, p2, q1)) 
 
      return true; 
 
    
 
     if (o2 == 0 && onSegment(p1, q2, q1)) 
 
      return true; 
 
    
 
     if (o3 == 0 && onSegment(p2, p1, q2)) 
 
      return true; 
 
    
 
     if (o4 == 0 && onSegment(p2, q1, q2)) 
 
      return true; 
 
    
 
     return false; 
 
    } 
 
    
 
    public static boolean isInside(Point polygon[], int n, Point p) 
 
    { 
 
     int INF = 10000; 
 
     if (n < 3) 
 
      return false; 
 
    
 
     Point extreme = new Point(INF, p.y); 
 
    
 
     int count = 0, i = 0; 
 
     do 
 
     { 
 
      int next = (i + 1) % n; 
 
      if (doIntersect(polygon[i], polygon[next], p, extreme)) 
 
      { 
 
       if (orientation(polygon[i], p, polygon[next]) == 0) 
 
        return onSegment(polygon[i], p, polygon[next]); 
 
    
 
       count++; 
 
      } 
 
      i = next; 
 
     } while (i != 0); 
 
    
 
     return (count & 1) == 1 ? true : false; 
 
    } 
 
    
 
    public static void main(String args[]) 
 
    { 
 
     Point polygon1[] = { new Point(0, 0), new Point(10, 0), 
 
       new Point(10, 10),new Point(5, 5), new Point(0, 10) }; 
 
     int n = 5; 
 
    
 
     Point p = new Point(5, 7); 
 
     System.out.println("Point P(" + p.x + ", " + p.y 
 
       + ") lies inside polygon1: " + isInside(polygon1, n, p)); 
 
     p = new Point(11, 11); 
 
     System.out.println("Point P(" + p.x + ", " + p.y 
 
       + ") lies inside polygon1: " + isInside(polygon1, n, p)); 
 
    
 
     Point polygon2[] = { new Point(0, 0), new Point(5, 5), new Point(5, 0) }; 
 
     n = 3; 
 
    
 
     p = new Point(3, 3); 
 
     System.out.println("Point P(" + p.x + ", " + p.y 
 
       + ") lies inside polygon2: " + isInside(polygon2, n, p)); 
 
     p = new Point(5, 1); 
 
     System.out.println("Point P(" + p.x + ", " + p.y 
 
       + ") lies inside polygon2: " + isInside(polygon2, n, p)); 
 
     p = new Point(8, 1); 
 
     System.out.println("Point P(" + p.x + ", " + p.y 
 
       + ") lies inside polygon2: " + isInside(polygon2, n, p)); 
 
    
 
     Point polygon3[] = { new Point(0, 0), new Point(10, 0), 
 
       new Point(10, 10), new Point(0, 10), new Point(5, 5) }; 
 
     n = 5; 
 
    
 
     p = new Point(-1, 10); 
 
     System.out.println("Point P(" + p.x + ", " + p.y 
 
       + ") lies inside polygon3: " + isInside(polygon3, n, p)); 
 
    } 
 
}

関連する問題