我尝试在googleMap上根据用户输入绘制多边形,但是我在网上看到的所有教程都是在init方法中完成的。我尝试在init状态之外实现它,但是似乎不起作用。请帮助!谢谢!
以下是将多边形添加到集合的方法:
Set<Polygon> _polygons = HashSet<Polygon>();
Future<void> drawPolygonBoundary(String name, List points) async {
List<LatLng> boundaryPoints = [];
for (List point in points) {
boundaryPoints.add(LatLng(point[0], point[1]));
}
Set<Polygon> newPolygons = HashSet<Polygon>();
newPolygons.add(
Polygon(
polygonId: PolygonId(name),
points: boundaryPoints,
fillColor: Colors.green.withOpacity(0.3),
strokeColor: Colors.green,
geodesic: true,
strokeWidth: 4,
onTap: () {},
),
);
//update polygons
setState(() {
_polygons = newPolygons.toSet();
});
print(_polygons);
}
这是我的GoogleMap小工具:
GoogleMap(
onMapCreated: _onMapCreated,
polygons: _polygons,
initialCameraPosition: CameraPosition(
target: _center,
zoom: 10.0,
),
myLocationEnabled: true,
myLocationButtonEnabled: false,
),
任何帮助将不胜感激!
1条答案
按热度按时间6l7fqoea1#
我开发了以下方法来绘制多边形: