Moh*_*med 7 geometry google-maps dart geofencing flutter
我正在使用 google-maps-flutter 插件处理 flutter 项目,我想检查用户位置是否在我在地图上创建的多边形内。有一种使用 JavaScript api(containsLocation() 方法)的简单方法,但是对于 flutter,我只找到了第三方插件 google_map_polyutil,它仅适用于 android,并且在我运行我的应用程序时遇到了安全问题。有没有别的办法呢??
小智 17
我找到了这个答案,只是修改了一些小东西以使用 dart,我对硬编码多边形进行了测试。列表 _area 是我的多边形,我的 mapcontroller 需要 _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)];
Run Code Online (Sandbox Code Playgroud)
函数是这样结束的:
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;
}
Run Code Online (Sandbox Code Playgroud)
注意polygons 属性和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);
},
)
Run Code Online (Sandbox Code Playgroud)
然后我只是在我的 _getAddress 方法中使用了以下调用:
_checkIfValidMarker(latLng, _area);
Run Code Online (Sandbox Code Playgroud)
我希望它可以帮助您创建所需的内容。
| 归档时间: |
|
| 查看次数: |
3350 次 |
| 最近记录: |