标记没有在谷歌地图上显示



我正在从服务器获取数据,并根据给定的数据显示标记。这是下面的代码,请告诉我我错在哪里。。

markers: markers,
myLocationEnabled: true,
//circles: mCircle,
onMapCreated: (GoogleMapController controller) {
_controller = controller;
_nearbyModelResponse.response.forEach((element) {
markers = Set.from([
Marker(position: LatLng(
double.parse(element.latitude),
double.parse(element.longitude)),
markerId: MarkerId(element.pcc),
icon: BitmapDescriptor.defaultMarker),
]);
//   marker.add();
});
},

您可以在build方法外创建一个自定义函数_onMapCreated,然后将其传递给GoogleMap小部件内的onMapCreated

final Map<String, Marker> _markers = {};
Future<void> _onMapCreated(GoogleMapController controller) async {
final _nearbyModelResponse = await {your method to fetch data from server}
setState(() {
_markers.clear();
for (final element in _nearbyModelResponse.response) {
final marker = Marker(
markerId: MarkerId(element.pcc),
position: LatLng(element.lat, element.lng),
icon: BitmapDescriptor.defaultMarker,
);
_markers[element.pcc] = marker;
}
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Markers Sample'),
),
body: GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: const LatLng(0, 0),
zoom: 2,
),
markers: _markers.values.toSet(),
),
),
);
}

相关内容

  • 没有找到相关文章

最新更新