Reputation: 476
I found a code in a tutorial that no longer works. After doing some research, I realized that the google maps api had been changed and the code was therefore no longer working. The tutorial is from the year 2018. Can anyone of you tell me how to write this code nowadays? The clearMarkers()
and the addMarker()
is red underlined.
The code:
void initMarker(client) {
mapController.clearMarkers().then((val) {
mapController.addMarker(
Marker(
position:
LatLng(client['location'].latitude, client['location'].longitude),
infoWindow: InfoWindow(title: client['name'])
),
);
});
}
Upvotes: 0
Views: 451
Reputation: 2757
First create a list of markers:
List<Marker> _markers = <Marker>[];
Then Fill the list, something like this:
//for 1..100
_markers.add(
Marker(
markerId: MarkerId("someId"),
infoWindow: InfoWindow(title: "Marker Title", snippet: "$snippet"),
position: LatLng(currentLocation.coordinates[1], currentLocation.coordinates[0]),
)
);
Now change your GoogleMaps to something like this:
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);
},
)
To clear the markers simply clear the marker list using a setState
setState(() {
_markers.clear();
});
Upvotes: 1