确定如果点在多边形 [英] Identify if point is in the polygon

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

问题描述

按我的要求,我绘制多边形下面的图片中显示谷歌地图。(使用地图V2)

As per my requirement, I am drawing polygons on google map shown in the image below.(using maps v2)

现在我需要显示警报,当用户输入特定的多边形。

Now I need to show an alert when user enters that particular polygons.

如何识别,如果我现在的位置是在多边形。 (需要在不耗尽电池优化的方式)

How to identify if my current location is with in the polygon. (Need optimized way without draining battery)

在此先感谢。

推荐答案

刚才试了光线投射算法识别的多边形点。这完美的作品。

Just tried Ray Casting algorithm which identifies point in polygon. This works perfect.

http://en.wikipedia.org/wiki/Point_in_polygon 雷的论文-Casting

Refer http://en.wikipedia.org/wiki/Point_in_polygon for thesis of Ray-Casting

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

这篇关于确定如果点在多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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