简体   繁体   English

在android中找到多边形中的一个点

[英]Finding a point in polygon in android

I am using the following code to find a point of coordinates exists in the code or not: 我使用以下代码来查找代码中是否存在坐标点:

mMap.setOnMapClickListener(new OnMapClickListener() 
{
    public void onMapClick(LatLng point) 
    {
        boolean checkPoly = true;
        Point2D[] points = new Point2D[ myPoints.size()];
        for ( int i = 0; i < myPoints.size(); i ++)
        {
            LatLng pt = myPoints.get(i);
            points[i] = new Point2D(pt.latitude, pt.longitude);
        }
        Polygon2D polygon2d = new SimplePolygon2D(points);

        double a = point.latitude;
        double b = point.longitude;
        Point2D myPt = new Point2D(a,b);

        checkPoly = polygon2d.contains(myPt);
        Log.i("CHECK", String.valueOf(checkPoly));
        if (checkPoly)
        {
            setMarker(point);
        }
        else
            Toast.makeText(NewSearch.this,"The Location is outside of the Area", Toast.LENGTH_LONG).show();
    }

I am using the JavaGeom 0.11.1 library for finding polygon point. 我正在使用JavaGeom 0.11.1库来查找多边形点。 However this code was working exactly fine. 但是这段代码工作正常。 Note that myPoints array is an ArrayList<LatLng> of all vertices of the polygons drawn on map. 请注意, myPoints数组是在地图上绘制的多边形的所有顶点的ArrayList<LatLng> However something happened and now it's working for opposite that is outside of map; 然而事情发生了,现在它正在为地图之外的对面工作; if i change !checkPoly then it works fine. 如果我改变了!checkPoly然后它工作正常。

Does anyone know what is wrong? 有谁知道什么是错的?

I looked at the source for the polygon boundary definition . 我查看了多边形边界定义源代码 It's using the usual convention for "inside," which requires the vertices to be given in CCW order around the "inside" space. 它使用通常的“内部”约定,它要求在“内部”空间周围以CCW顺序给出顶点。 It's likely your boundary is given in CW order, which makes the "inside" what most people would call the outside. 很可能你的边界是按CW顺序给出的,这使得大多数人称之为“内部”的是外部。

In other words, what you think is a polygon is really a hole in the infinite polygon that covers the whole xy universe. 换句话说,你认为多边形实际上是覆盖整个xy宇宙的无限多边形中的一个洞。

So reverse the order of boundary vertices and things should start working as you intend. 因此,颠倒边界顶点的顺序,事情应该按照您的意图开始工作。

ADDITION 加成

If you can't reverse the order of vertices, there is a different polygon membership test that doesn't rely on point order. 如果无法反转顶点的顺序,则会有一个不依赖于点顺序的不同多边形成员资格测试。 If you are testing membership of the point (x,y), this algorithm assumes that the point (infinity, y) is outside the polygon and then decides whether (x,y) is on the opposide side. 如果您正在测试点(x,y)的成员资格,则此算法假定点(无穷大,y)在多边形之外,然后决定(x,y)是否在对侧。 The implementation here in C is due to WR Franklin . C中实现归功于WR Franklin It would be easy to port this to Java. 将它移植到Java很容易。 I've used it several times with excellent results. 我已经好几次使用它了,效果很好。

我一直在使用谷歌android-maps-utils库 ,你可以使用PolyUtil类,特别是在这个方法:

public static boolean containsLocation(LatLng point, List<LatLng> polygon, boolean geodesic)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM