【发布时间】:2019-09-23 01:28:31
【问题描述】:
我正在使用 Flutter 制作一个使用地图的移动应用程序。我们决定使用谷歌地图,我们使用的颤振插件是:
google_maps_flutter:^0.5.7
我了解向地图添加标记的工作方式如下:
Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
Marker _marker = new Marker(
icon: BitmapDescriptor.defaultMarker,
markerId: _markerId,
position: LatLng(34.024441,-5.0310968),
infoWindow: InfoWindow(title: "userMarker", snippet: '*'),
);
GoogleMap _map;
@override
void initState(
markers[_markerId] = _marker;
_map = new GoogleMap(
/* other properties irrelevant to this prob */
markers: Set<Marker>.of(_markers.values),
);
);
上面的 sn-p 确实有效,我可以在地图上看到标记。但是修改标记或尝试像下面的 sn-p 那样添加另一个标记不起作用。
FloatingActionButton(
onPressed: () {
setState(() {
_marker = new Marker(
icon: BitmapDescriptor.defaultMarker,
markerId: _markerId,
position: LatLng(currentLocation.latitude, currentLocation.longitude),
infoWindow: InfoWindow(title: "userMarker", snippet: '*'),
onTap: () {
debugPrint("Marker Tapped");
},
);
markers[_markerId] = _marker; // What I do here is modify the only marker in the Map.
});
markers.forEach((id,marker) { // This is used to see if the marker properties did change, and they did.
debugPrint("MarkerId: $id");
debugPrint("Marker: [${marker.position.latitude},${marker.position.longitude}]");
});
});
)
我的意图是使用另一个插件 (geoLocator) 来获取用户的位置数据并更改我拥有的唯一标记,以便它可以跟踪他的动作。 debugPrint 显示数据确实在发生变化,但我在地图中看不到任何变化(我更改的初始标记使用的位置与我测试时使用的位置不同)。
【问题讨论】:
-
您在第一个代码 sn-p 中没有 _markers,但您在 Set
.of(_markers.values) 行上使用,这让我认为您的问题可能与命名有关 -
我只是错误地复制了错误的名称。我实际上将标记对象传递给我的自定义地图类构造函数,其中包含 _markers 属性。会不会是引用的问题?因为我将标记传递给构造函数,但我不知道 Dart 是如何引用对象的。
标签: google-maps flutter google-maps-markers