使用android中的位置类指向多边形算法?

时间:2013-08-13 01:02:26

标签: android point-in-polygon

我有一组形成封闭路径的位置(类似于多边形)。有没有办法检查某个纬度和经度是否在封闭路径内?

4 个答案:

答案 0 :(得分:4)

如果对边角情况没有限制,可以将位置列表添加到LatLngBounds,然后使用bounds.contains(lat,lng)。

LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(new LatLng (52.39455,13.73096));
builder.include(new LatLng (52.39650,13.71644));   
builder.include(new LatLng (52.38719,13.69940));
LatLngBounds bound = builder.build();

if (bounds.contains(lat,lng)
// your code goes here

注意:如果您想确切地确定该点是否在多边形内,这将不起作用,因为LatLngBounds将是一个包围由您的位置列表形成的多边形的框。

答案 1 :(得分:3)

更好的方式,

 ArrayList<LatLng> polyLoc = new ArrayList<LatLng>();

    public boolean Contains(LatLng location)
        {
            if (location==null)
                return false;

            LatLng lastPoint = polyLoc.get(polyLoc.size()-1);
            boolean isInside = false;
            double x = location.longitude;

            for(LatLng point: polyLoc)
            {
                double x1 = lastPoint.longitude;
                double x2 = point.longitude;
                double dx = x2 - x1;

                if (Math.abs(dx) > 180.0)
                {
                    // we have, most likely, just jumped the dateline (could do further validation to this effect if needed).  normalise the numbers.
                    if (x > 0)
                    {
                        while (x1 < 0)
                            x1 += 360;
                        while (x2 < 0)
                            x2 += 360;
                    }
                    else
                    {
                        while (x1 > 0)
                            x1 -= 360;
                        while (x2 > 0)
                            x2 -= 360;
                    }
                    dx = x2 - x1;
                }

                if ((x1 <= x && x2 > x) || (x1 >= x && x2 < x))
                {
                    double grad = (point.latitude - lastPoint.latitude) / dx;
                    double intersectAtLat = lastPoint.latitude + ((x - x1) * grad);

                    if (intersectAtLat > location.latitude)
                        isInside = !isInside;
                }
                lastPoint = point;
            }

            return isInside;
        }

请参阅以下链接了解实际帖子, Checking if a longitude/latitude coordinate resides inside a complex polygon in an embedded device?

答案 2 :(得分:2)

您需要一个光线投射算法。对你有好处,有人已经这样做了:; - )

Point in Polygon Algorithm

还有一点背景知识:

https://en.wikipedia.org/wiki/Point_in_polygon

答案 3 :(得分:1)

使用光线投射algoritham.Below是用来做那个的java代码

public  boolean CheckPointInPolyGon(LocObj locObj,ArrayList<LocObj> ArraylocObjs){
int polyCorners=ArraylocObjs.size()-1;
    int j=polyCorners-1;
    boolean isPointInPolygon=false;


    for (int i=0; i<polyCorners; i++) 
      {   
          if ((ArraylocObjs.get(i).lng< locObj.lng && ArraylocObjs.get(j).lng>=locObj.lng   
                  ||   ArraylocObjs.get(j).lng< locObj.lng && ArraylocObjs.get(i).lng>=locObj.lng)      
                  &&  (ArraylocObjs.get(i).lat<=locObj.lat || ArraylocObjs.get(j).lat<=locObj.lat)) 
          {
              if (ArraylocObjs.get(i).lat+(locObj.lng-ArraylocObjs.get(i).lng)/(ArraylocObjs.get(j).lng-ArraylocObjs.get(i).lng)
                      *(ArraylocObjs.get(j).lat-ArraylocObjs.get(i).lat)<locObj.lat) 
              {
                isPointInPolygon=!isPointInPolygon; 
              }
          }
          j=i; 
      } 


return isPointInPolygon;}

在我的情况下,我创建了一个用于存储此位置点的POJO类 如果你想要你可以使用&#34; LatLng&#34;用于存储积分的类

public class LocObj{Double lat,lng; 
public LocObj(){}   
LocObj(Double lat,Double lng){
    this.lat=lat;
    this.lng=lng;
    }}