【问题标题】:Show multiple markers in google maps在谷歌地图中显示多个标记
【发布时间】:2020-08-31 18:26:15
【问题描述】:

我正在尝试为谷歌地图创建多个标记。我从 Set<Marker> markers = Set(); 开始,然后在 List 上循环并将标记添加到 markers

Marker resultMarker = Marker(
            markerId: MarkerId(currentLocation.id),
            infoWindow: InfoWindow(title: "${currentLocation.title}", snippet: "$snippet"),
            position: LatLng(currentLocation.coordinates[1], currentLocation.coordinates[0]),
          );

markers.add(resultMarker);

然后我尝试返回一个小部件:

GoogleMap(
          initialCameraPosition: CameraPosition(target: _cameraPosition, zoom: 11.0),
          onMapCreated: (controller) => mapController = controller,
          myLocationButtonEnabled: false,
          markers: markers,
);

但我只得到第一个标记,而不是全部。如何获取所有标记?

【问题讨论】:

  • 你确定markers.length不止一个吗?
  • @P4yam 是的。现在是 3
  • 只是在黑暗中拍摄的完整照片,但如果这些值出于某种原因是异步的并且它们都在当前位置,那么它们可能都在更新到最新的当前位置。这意味着它们可能会显示,但只是在彼此之上。我不知道您是否将三个结果标记添加到标记列表中,或者您是否有两个预定义的标记。

标签: google-maps flutter dart


【解决方案1】:

这个对我有用:

List<Marker> markers = <Marker>[];

在你的循环中:

    markers.add(
      Marker(
        markerId: MarkerId(currentLocation.id),
        infoWindow: InfoWindow(title: "${currentLocation.title}", snippet: "$snippet"),
        position: LatLng(currentLocation.coordinates[1], currentLocation.coordinates[0]),

     )
   );

确保标记数据正确,因为谷歌地图不会显示带有无效LatLng 的标记(尝试使用虚拟数据进行测试)

然后

GoogleMap(
      initialCameraPosition: CameraPosition(
        target: LatLng(38.9647,35.2233),
        zoom: 9.0,
      ),
      mapType: MapType.normal,
      markers: Set<Marker>.of(_markers),
      onMapCreated: (GoogleMapController controller) {
        _controller.complete(controller);
      },
    )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-01
    • 2016-02-04
    • 2020-06-08
    • 1970-01-01
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多