使用Android中的Google Maps API检查点是否为多边形

我在 Android上使用Google Maps API来制作拼图.此链接包含我用于绘制非洲国家的数据: World countries coordinates.

用户点击地图时,会进行测试以检查它是否在正确的国家/地区.

>指向正确的国家:正确的国家是绿色的
>指向另一个已知国家:当前国家的颜色为红色

下面的代码迭代非洲国家/地区列表(一个国家可能包含多个多边形),以找到包含点击点的那个,并将其与正确的国家进行比较.

mMap.setonMapClickListener(new GoogleMap.OnMapClickListener() {
    @Override
    public void onMapClick(LatLng latLng) {

        if(isPointInpolygon(latLng,answerpolygon)) {
            // right answer
            Toast.makeText(MapsActivity.this,"point inside the right polygon",Toast.LENGTH_SHORT).show();
            polygon p = mMap.addpolygon(new polygonoptions().addAll(answerpolygon));
            p.setstrokeWidth(p.getstrokeWidth()/5);
            p.setFillColor(0x7F00FF00);
        }
        else {
            // wrong answer
            // search current polygon
            // color current polygon in red
            if (colorpolygonInRed(latLng)){
                polygon p = mMap.addpolygon(new polygonoptions().addAll(answerpolygon));
                p.setstrokeWidth(p.getstrokeWidth()/5);
                p.setFillColor(0x7F00FF00);
                Toast.makeText(MapsActivity.this,"point in kNown polygons",Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(MapsActivity.this,"point in unkNown polygons",Toast.LENGTH_SHORT).show();
            }
        }
    }
});

函数isPointInpolygon()仅在地图上使用一个多边形,因为它基于几何(光线相交).它在我的情况下不起作用.
例如,当我点击Chad国家(this picture中的蓝点)时,埃及变成红色(我的列表按字母顺序排序,因此埃及是点击点右侧的第一个验证光线交叉条件的点).

public 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;
}
public boolean colorpolygonInRed(LatLng point){
    for (Country country:countryList){
        for (ArrayList<LatLng> polygon : country.getCoordinates()){
            if(isPointInpolygon(point,polygon)) {
                polygon p = mMap.addpolygon(new polygonoptions().addAll(polygon));
                p.setstrokeWidth(p.getstrokeWidth()/5);
                p.setFillColor(0x7FE00808);
                return true;
            }
        }
    }
    return false;
}

获取从列表中单击的多边形的正确方法是什么?

解决方法

您可以使用 Google Maps Android API Utility Library中的polyUtil.containsLocation方法.从 the documentation开始:

public static boolean containsLocation(LatLng point,
java.util.List polygon,
boolean geodesic)

Computes whether the given point lies inside the specified polygon. The polygon is always considered closed,regardless of whether the last point equals the first or not. Inside is defined as not containing the South Pole — the South Pole is always outside. The polygon is formed of great circle segments if geodesic is true,and of rhumb (loxodromic) segments otherwise.

相关文章

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