in the google maps plugin for flutter it is posable to draw a polygon shape using a list of points.
here is an example of creating a polygon in the shape of a circle around a given center with a radius of 1000 meters using 50 points:
@override
Widget build(BuildContext context) {
final List<LatLng> polygonCoordinates1 = <LatLng>[];
final int points = 50;
double degreesPerPoint = 360 / points;
final double radius = 1000;
final LatLng center1 = LatLng(37.422, -122.084);
for (int i = 0; i < points; i++) {
double radians = degreesPerPoint * i * Math.pi / 180;
double lat1 = center1.latitude + radius / 111000 * Math.cos(radians);
double lng1 = center1.longitude + radius / (111000 * Math.cos(center1.latitude * Math.pi / 180)) * Math.sin(radians);
polygonCoordinates1.add(LatLng(lat1, lng1));
}
Polygon circle1 = Polygon(
polygonId: PolygonId('myCircle1'),
points: polygonCoordinates1,
strokeColor: Colors.red,
strokeWidth: 2,
fillColor: Colors.blue.withOpacity(0.5),
);
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: GoogleMap(
polygons: <Polygon>{ circle1},
initialCameraPosition: const CameraPosition(
target: LatLng(37.422, -122.084),
zoom: 15,
),
),
),
);
}
my problem is when trying to create a single polygon using 2 or more centers. in other words as if trying to show an area a user covered while walking.
combining all points of both circles isn’t working. it is also drawing the overlapping points. how do I get a list of points in the right order that aren’t overlapping?
2
Answers
Try This One