I'm using google_maps_flutter: ^0.5.13
to develop a basic 'multiple markers on a google map' type Flutter screen.
I am curious how others set the initialCameraPostion
for the GoogleMap()
class. I either
In the getMapMarkers()
function, I will call a setMarkers(List<dynamic> markers)
(that takes in a list of the markers or objects) and grab one of the GeoPoint
from the list of markers and use that to set the initialCameraPosition
.
class MapScreenState extends State<MapScreen> {
GoogleMapController _mapController;
// Method #1
static CameraPosition _initialCameraPosition =
CameraPosition(target: const LatLng(26.357540, -81.785290), zoom: 12);
void _onMapCreated(GoogleMapController controller) {
_mapController = controller;
_getMapMarkers();
}
@override
Widget build(BuildContext context) {
return Stack(children: <Widget>[_googleMap(context)]);
}
Widget _googleMap(BuildContext context) {
return Column(children: <Widget>[
Expanded(
child: Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: GoogleMap(
initialCameraPosition: _initialCameraPosition,
onMapCreated: _onMapCreated,
mapType: MapType.normal,
markers: Set<Marker>.of(markers.values),
),
),
)
]);
}
}
// Method #2
void _setCenter(List<dynamic> markers) {
GeoPoint geo = markers[0].geocode;
setState(() {
_initialCameraPosition =
CameraPosition(target: LatLng(geo.latitude, geo.longitude), zoom: 12);
});
}
To move the camera instantly with the given CameraUpdate , you can call GoogleMap. moveCamera(CameraUpdate) . The CameraUpdate describing where to move the camera.
You can add a simple marker to the map at a desired location by instantiating the marker class and specifying the position to be marked using latlng, as shown below.
The transition() and moveMarker() are used to move marker smoothly on click on the Google map.
Google Maps Flutter module is given in the Google Map widget that upholds initialCameraPosition, maptype and onMapCreated. We can set the situation of the camera and marker in any put on the earth. We can plan the marker as per our decision.
In the getMapMarkers () function, I will call a setMarkers (List<dynamic> markers) (that takes in a list of the markers or objects) and grab one of the GeoPoint from the list of markers and use that to set the initialCameraPosition.
We’re telling fluster to get the cluster markers within the giving bounding box of [ westLng, southLat, eastLng, northLat] for the current zoom level and converting them to Google Maps Markers. Now we can use that list to show the markers on the map. 5. Show the Map with Markers
We can set the situation of the camera and marker in any put on the earth. We can plan the marker as per our decision. It additionally accompanies a zoom property in a cameraposition to give the zooming in google map see on the underlying page.
initialCameraPosition
only for the initial time. You can move the camera using the following way. We already got a camera controller using onMapCreated
hook.
void _onMapCreated(GoogleMapController controller) {
_mapController = controller;
_getMapMarkers();
}
Using This _mapController
we can access moveCamera
function and move the camera to marker position.
_mapController.moveCamera(CameraUpdate.newLatLng(<LatLng one of your marker position>));
Example:
_mapController.moveCamera(CameraUpdate.newLatLng(LatLng(markerLat,markerLng)));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With