android – 识别点是否在多边形中

根据我的要求,我在下面的图片中绘制的谷歌地图上绘制多边形(使用地图v2)

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

如何识别我的当前位置是否在多边形中.
(需要优化的方式,不排出电池)

提前致谢.

解决方法

刚刚尝试的Ray Casting算法可以识别多边形中的点.这是完美的.

参考http://en.wikipedia.org/wiki/Point_in_polygon论文的射线铸造

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

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...