Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center Camera Position around Google Maps Markers using Flutter

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

  1. Set it static initially
  2. 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);
      });
    }
    
like image 933
MaylorTaylor Avatar asked May 25 '19 14:05

MaylorTaylor


People also ask

How do I move the camera on Google Maps?

To move the camera instantly with the given CameraUpdate , you can call GoogleMap. moveCamera(CameraUpdate) . The CameraUpdate describing where to move the camera.

How do I get a marker position on Google Maps?

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.

How do I move a marker smoothly on Google Maps?

The transition() and moveMarker() are used to move marker smoothly on click on the Google map.

What is the Google Maps flutter module?

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.

How to set the initial camera position using getmapmarkers?

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.

How to show cluster markers on Google Maps with fluster?

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

How to zoom in Google map see on underlying page?

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.


1 Answers

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)));
like image 75
Leo ananth Avatar answered Oct 18 '22 04:10

Leo ananth