如何在谷歌地图上添加多边形?

时间:2019-07-02 09:27:35

标签: android google-maps flutter dart

我正在使用google_maps_flutter插件来使用Google地图。我想在Google地图上绘制一个具有用户坐标的多边形。采取的坐标是“位置”对象的类型。

我已经尝试使用map_view插件绘制多边形。但这对我没有用。

这是获取坐标的代码段。

Geolocator geolocator = Geolocator();
StreamSubscription<Position> _positionStreamSubscription;
final List<Position> _positions = <Position>[];

void _toggleListening() {
if (_positionStreamSubscription == null) {
  const LocationOptions locationOptions =
      LocationOptions(accuracy: LocationAccuracy.best, distanceFilter: 10);
  final Stream<Position> positionStream =
      Geolocator().getPositionStream(locationOptions);
  _positionStreamSubscription = positionStream.listen(
      (Position position) => setState(() => _positions.add(position)));
  _positionStreamSubscription.pause();
}

setState(() {
  if (_positionStreamSubscription.isPaused) {
    _positionStreamSubscription.resume();
  } else {
    _positionStreamSubscription.pause();
  }
});

1 个答案:

答案 0 :(得分:1)

好吧,如果您可以将$authenticationState对象的坐标转换为Position对象,则可以使用google_maps_flutter本身绘制多边形。您可以看到one of its parameters允许Polygons。因此,仅以google_maps_flutter中的示例为基础,我们将首先设置多边形:

LatLng

然后将其提供给GoogleMap小部件。

Set<Polygon> myPolygon() {
  List<LatLng> polygonCoords = new List();
  polygonCoords.add(LatLng(37.43296265331129, -122.08832357078792));
  polygonCoords.add(LatLng(37.43006265331129, -122.08832357078792));
  polygonCoords.add(LatLng(37.43006265331129, -122.08332357078792));
  polygonCoords.add(LatLng(37.43296265331129, -122.08832357078792));

  Set<Polygon> polygonSet = new Set();
  polygonSet.add(Polygon(
      polygonId: PolygonId('test'),
      points: polygonCoords,
      strokeColor: Colors.red));

  return polygonSet;
}

这将在GooglePlex附近绘制一个小三角形。

有关此功能的更多信息,您可以参考Maps SDK Shapes的Android文档。 API的用法应该相同,即使不相似,也可以作为指导。