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

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

概述我在 Android上使用Google Maps API来制作拼图.此链接包含我用于绘制非洲国家的数据: World countries coordinates. 当用户点击地图时,会进行测试以检查它是否在正确的国家/地区. >指向正确的国家:正确的国家是绿色的 >指向另一个已知国家:当前国家的颜色为红色 下面的代码迭代非洲国家/地区列表(一个国家可能包含多个多边形),以找到包含点击点的那个,并将 我在 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中的Google Maps API检查点是否为多边形全部内容,希望文章能够帮你解决使用Android中的Google Maps API检查点是否为多边形所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1122563.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存