检查Latlng是否在多边形内 [英] check if Latlng is inside polygon

查看:150
本文介绍了检查Latlng是否在多边形内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在城市绘制多边形以检查当前位置是否在这个多边形内,并且我用下面的代码来做这件事: - $ / $>

  ArrayList< LatLng> polyLoc = new ArrayList< LatLng>(); 
polyLoc.add(new LatLng(24.643932,46.297718));
polyLoc.add(new LatLng(24.695098,46.555897));
polyLoc.add(new LatLng(24.921971,46.476246));
polyLoc.add(新LatLng(25.147185,46.366383));
polyLoc.add(new LatLng(25.155886,47.249409));
polyLoc.add(new LatLng(24.929444,47.346913));
polyLoc.add(new LatLng(24.691355,47.106587));
polyLoc.add(new LatLng(24.449060,47.219197));
polyLoc.add(new LatLng(24.293947,46.973377));
polyLoc.add(new LatLng(24.641436,46.299092));

我通过这种方式检查当前位置是否在这个多边形之内: -

  if(hasPermission()&& gpsTracker.canGetLocation()){
if(isPointInPolygon(new LatLng(gpsTracker。 getLatitude(),gpsTracker.getLongitude()),polyLoc)){
cash.setVisibility(View.VISIBLE);
cashIcon.setVisibility(View.VISIBLE);
} else {
cash.setVisibility(View.GONE);
cashIcon.setVisibility(View.GONE);
}
Log.d(TAG,reservationDialog:+ gpsTracker.getLatitude()+ gpsTracker.getLongitude());
}

这里是我的isPointInPolygon方法:

  private boolean isPointInPolygon(LatLng tap,ArrayList< LatLng> vertices){
int intersectCount = 0; (j = 1,j if(rayCastIntersect(tap,vertices.get(j),vertices.get(j + 1) )){
intersectCount ++;
}
}

return((intersectCount%2)== 1); // odd = inside,even = outside;
}

私有布尔值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; (aY> pY& bY> pY)||(aY< pY& bY< pY)
||(aX< pX&& bX< pX)){
return false; // a和b不能同时高于或低于pt.y,并且a或
// b必须位于pt.x的东部b

$ b double m = (aY-bY)/(aX-bX); //超过运行
double bee =(-aX)* m + aY; // y = mx + b
double x =(pY - bee)/ m; //代数很整洁!

return x>的pX;
}

我不知道为什么它不起作用,我在这里错过了什么?

解决方案

我知道为什么你的代码无法工作。

<但是您可以使用 PolyUtil.containsLocation(new Latlng(latitude,longitude),polyLoc,true)



如果位于多边形之外,将返回false。


I'm drawing Polygon on city in order to check if current position is inside this polygon or not, and i'm doing that with below code:-

 ArrayList<LatLng> polyLoc = new ArrayList<LatLng>();
polyLoc.add(new LatLng(24.643932, 46.297718));
    polyLoc.add(new LatLng(24.695098, 46.555897));
    polyLoc.add(new LatLng(24.921971, 46.476246));
    polyLoc.add(new LatLng(25.147185, 46.366383));
    polyLoc.add(new LatLng(25.155886, 47.249409));
    polyLoc.add(new LatLng(24.929444, 47.346913));
    polyLoc.add(new LatLng(24.691355, 47.106587));
    polyLoc.add(new LatLng(24.449060, 47.219197));
    polyLoc.add(new LatLng(24.293947, 46.973377));
    polyLoc.add(new LatLng(24.641436, 46.299092));

And i checking if the current position is inside this polygon or not by this way :-

if (hasPermission() && gpsTracker.canGetLocation()) {
        if (isPointInPolygon(new LatLng(gpsTracker.getLatitude(), gpsTracker.getLongitude()), polyLoc)) {
            cash.setVisibility(View.VISIBLE);
            cashIcon.setVisibility(View.VISIBLE);
        } else {
            cash.setVisibility(View.GONE);
            cashIcon.setVisibility(View.GONE);
        }
        Log.d(TAG, "reservationDialog: " + gpsTracker.getLatitude() + gpsTracker.getLongitude());
    }

here is my isPointInPolygon method :

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;
}

I don't know why it's not working, what i'v missed here?

解决方案

I dun know why your code didn't work.

But you can use PolyUtil.containsLocation (new Latlng (latitude, longitude), polyLoc, true)

Will return false if the position outside the polygon.

这篇关于检查Latlng是否在多边形内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆