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

问题描述

我正在使用google-maps-flutter插件开发Flutter项目,我想检查用户位置是否在我在地图上创建的多边形内。有一种使用JavaScript api(con​​tainsLocation()方法)的简单方法,但对于Flutter,我仅发现了第三方插件google_map_polyutil,该插件仅适用于android,并且在运行我的应用程序时出现安全漏洞。还有另一种方法吗?

I'm working on flutter project using google-maps-flutter plugin, and I want to check if the user location is inside the polygon that I created on the map. There is an easy way using JavaScript api (containsLocation() method) but for flutter I only found a third party plugin,google_map_polyutil, which is only for android and I get a security worming when I run my app. Is there another way to do so??

推荐答案

我发现,并且只是修改了一些小东西以使用dart,我对一个硬编码的多边形进行了测试。列表_area是我的多边形,而_polygons是我的地图控制器所必需的。

I found this answer and just modified some minor things to work with dart, I ran a test on a hardcoded polygon. The list _area is my polygon and _polygons is required for my mapcontroller.

final Set<Polygon> _polygons = {};
List<LatLng> _area = [
LatLng(-17.770992200, -63.207739700),
LatLng(-17.776386600, -63.213576200),
LatLng(-17.778348200, -63.213576200),
LatLng(-17.786848100, -63.214262900),
LatLng(-17.798289700, -63.211001300),
LatLng(-17.810547700, -63.200701600),
LatLng(-17.815450600, -63.185252100),
LatLng(-17.816267800, -63.170660900),
LatLng(-17.800741300, -63.153838100),
LatLng(-17.785867400, -63.150919800),
LatLng(-17.770501800, -63.152636400),
LatLng(-17.759712400, -63.160361200),
LatLng(-17.755952300, -63.169802600),
LatLng(-17.752519100, -63.186625400),
LatLng(-17.758404500, -63.195551800),
LatLng(-17.770992200, -63.206538100),
LatLng(-17.770996000, -63.207762500)];

函数结束如下:

bool _checkIfValidMarker(LatLng tap, List<LatLng> vertices) {
    int intersectCount = 0;
    for (int j = 0; j < vertices.length - 1; j++) {
      if (rayCastIntersect(tap, vertices[j], vertices[j + 1])) {
        intersectCount++;
      }
    }

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

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

请注意多边形属性和onTap方法。我正在尝试检查在我的地图中创建的标记是否在我的多边形内:

Notice the polygons property and the onTap method. I was trying to check if the marker created in my map was inside my polygon:

GoogleMap(
                          initialCameraPosition: CameraPosition(
                            target: target, //LatLng(0, 0),
                            zoom: 16,
                          ),
                          zoomGesturesEnabled: true,
                          markers: markers,
                          polygons: _polygons,
                          onMapCreated: (controller) =>
                              _mapController = controller,
                          onTap: (latLng) {
                            _getAddress(latLng);
                          },
                        )

然后我在_getAddress方法中使用了以下调用:

Then i just used the following call in my _getAddress method:

_checkIfValidMarker(latLng, _area);

我希望它可以帮助您创建所需的内容。

I hope it helps you to create what you need.

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