google-maps - 谷歌地图颤动检查多边形内的点
问题描述
我正在使用 google-maps-flutter 插件处理颤振项目,我想检查用户位置是否在我在地图上创建的多边形内。有一种使用 JavaScript api(containsLocation() 方法)的简单方法,但对于颤振,我只找到了一个第三方插件,google_map_polyutil,它仅适用于 android,当我运行我的应用程序时,我得到了一个安全蠕虫。还有其他方法吗?
解决方案
我找到了这个答案,只是修改了一些小东西来使用飞镖,我对硬编码的多边形进行了测试。列表 _area 是我的多边形,我的地图控制器需要 _polygons。
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 方法。我试图检查在我的地图中创建的标记是否在我的多边形内:
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 方法中使用了以下调用:
_checkIfValidMarker(latLng, _area);
我希望它可以帮助您创建所需的内容。
推荐阅读
- c++ - Matlab 编码器:如何强制变量具有变量:inf 大小
- python-3.x - python pandas异常值删除-由于错误而无法删除
- azure-data-factory - Azure 数据工厂:使用复制活动设置复制文件数量的限制
- javascript - 将标记添加到传单中的用户位置
- ckeditor - Typo3 关闭并开始一个新段落
在每个
新闻扩展的正文中 - openmdao - 什么时候适合使用元模型而不是昂贵的分析?
- c - 在 Linux(RedHat) 中,C 函数 malloc_stats() 显示与 /proc/ 不同的值
/stat 常驻内存大小 - javascript - 一起使用 scroll-ID DOM-event
- python - Pandas:将年度数据转换为十年数据
- flutter - 在 null 上调用了方法“_mulFromInteger”