i am new to flutter and i am trying to let users define zones in gmap by using marks to draw polygons. Example of a zone, with 3 markers/point
So, now what i want is to check if the user is not trying to do some like crossing the lines like this: Check for this not to happen
the numbers shown in the markers are not relavant, so i could do some math between coordinates and just redraw the polygon, but i wanted to avoid that just cause im lazy =)
and i noticed that when you cross the lines the polygon is not filled anymore, so it has some kind of internal check, that i would like to access if possible.
I am using Gmap and drawing the polygons with following code:
...
Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
int lastMarkerID = 0;
Set<Polygon> _polygons = HashSet<Polygon>();
...
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(title: Text('Add Zone')),
body: Stack(
children: <Widget>[
GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: _kGoogleInitialCamera,
onLongPress: (latlang) {
_addMarkerLongPressed(latlang);
},
markers: Set<Marker>.of(markers.values),
polygons: _polygons,
polylines: _polylines,
),
],
),
);
}
...
Future _addMarkerLongPressed(LatLng latlong, [_lastMarkerID]) async {
_lastMarkerID ?? lastMarkerID++;
_lastMarkerID = _lastMarkerID ?? lastMarkerID;
final MarkerId markerId = MarkerId(_lastMarkerID.toString());
Marker marker = Marker(
markerId: markerId,
draggable: true,
position: latlong,
onDragEnd: ((value) {
_addMarkerLongPressed(value, _lastMarkerID);
}),
infoWindow: InfoWindow(
title: markerId.value,
),
consumeTapEvents: true,
onTap: _showDialog(
context, <String>["Delete Marker"], _lastMarkerID, _deleteMarker),
icon: await createCustomMarkerBitmap(markerId.value),
);
markers[markerId] = marker;
_setPolygons();
setState(() {});
}
...
void _setPolygons() {
_polygons.clear();
List<LatLng> polygonLatLongs = List<LatLng>();
markers.forEach((key, value) {
Commons.log(value.position);
polygonLatLongs.add(value.position);
});
if (polygonLatLongs.length >= 3) {
_setPolylines(polygonLatLongs);
polygonLatLongs.add(polygonLatLongs.first);
}
_polygons.add(
Polygon(
polygonId: PolygonId("0"),
points: polygonLatLongs,
fillColor: Commons.colorPolygonAddZone,
strokeColor: Commons.colorPolygonStrokeAddZone,
strokeWidth: Commons.strokeWidthPolygonAddZone,
),
);
}
As i said i'm new to this so feel free to sugeste other ways to achive this.
I could only find posts about check if a point is inside the polygon sorry if it's repeated question.