Anoop
Anoop

Reputation: 146

How to draw polygon in Google map Flutter dynamically

I need to create polygons on the Google map dynamically in flutter. I could plot polygons with known points but I couldn't draw the polygons dynamically Please help...

Upvotes: 3

Views: 2084

Answers (1)

Sucper
Sucper

Reputation: 21

I also need to draw a polygon point by point. To do this, in the onTap handler

GoogleMap(
    polygons: Set<Polygon>.of(mapsPolygons.values),
    onTap: tapMap,
    ...
)

I add a marker and write each tap down to the List<LatLng>. Then I update the map, with the rendered polygon

Map<PolygonId, Polygon> mapsPolygons = <PolygonId, Polygon>{};
List<LatLng> mapsPolygons = [];

void tapMap(LatLng pos) {
  final PolygonId polygonId = PolygonId("Polgon_1");
  final Polygon polygon = Polygon(
    polygonId: polygonId,
    strokeColor: Colors.red,
    strokeWidth: 5,
    fillColor: Colors.red.withOpacity(0.3),
    points: polygonPoints,
  );
  mapsPolygons[polygonId] = polygon;
  
  //setState((){});
}

i update the map using StreamBuilder() but it should work with setState() too

example

P.S. Sorry for my English

Upvotes: 1

Related Questions