判断一个点在圆内,百度地图判断点是否在圈内,多边形内,区域内,

最近项目做GIS地图应用,需求是判断某个点是否在圈内,多边形内,区域内,网上找了好久都没找到合适的方法,看百度demo时,看着某个api特别像有能力的样子,试了一下,还真成功了。

 

关键类:LatLngBounds

关键方法:

public boolean contains(LatLng var1) //LatLngBounds的判断点在范围内的方法

 

private List getPointInPolygon(int count, List points) {
    if (points.size() < 3) {
        return null;//不是多边形
    }
    double x1 = Double.MAX_VALUE;
    double x2 = Double.MIN_VALUE;
    double y1 = Double.MAX_VALUE;
    double y2 = Double.MIN_VALUE;

    for (LatLng p : points) {
        if (p.latitude < x1)
            x1 = p.latitude;
        if (p.latitude > x2)
            x2 = p.latitude;
        if (p.longitude < y1)
            y1 = p.longitude;
        if (p.longitude > y2)
            y2 = p.longitude;
    }
    //int ranNumber =ran.nextInt(max - min + 1) + min; // ranNumber 将被赋值为一个 min 和 max 范围内的随机数[min,max]
    Random rnd = new Random();
    List pointRet = new ArrayList<>();

    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    for (LatLng point : points) {
        builder.include(point);
    }

    LatLngBounds latLngBounds = builder.build();


    int n = 0;
    while (n < 3) {
        double la = Math.random() * (x2 - x1) + x1;
        double ln = Math.random() * (y2 - y1) + y1;
        LatLng p = new LatLng(la, ln);
        if (latLngBounds.contains(p)) {
            pointRet.add(p);
            n++;
        }
    }
    return pointRet;
}

 

实现步骤:

1.LatLngBounds.Builder通过include 加入已知范围内的点

2.LatLngBounds.Builder的build方法创建LatLngBounds,表示一个坐标边界范围

3.判断坐标是否在范围内,latLngBounds.contains(p)

4.完成功能。

 

 

废话不多说,直接贴代码

你可能感兴趣的:(工作总结,Android,地图,GIS,百度,Android)