0

I am using Stream Builder that sends api request after some seconds and fetch coordinates (lat, lng).

I want to update the map location (camera position & marker) on newly fetch coordinates.

But Camera position is updating and not focusing on new coordinates. My google map widget is inside of stream builder and in which we are passing data through snapshot e.g latlng = LatLng(snapshot.data['lat'],snapshot.data['lng']) to markers: SetMarker( markers, markerID, latlng, ), & initialCameraPosition: CameraPosition( target: latlng, zoom: 9.0, )

1 Answers1

1

The GoogleMaps widget on Flutter does have the attribute initialCameraPosition but as the name says this is just an initial value, if you want to update the map once this has loaded i'd recommend the use of the callback onMapCreated, where you can use the controller to animate to a certain position.

Doing so would look something like this:

onMapCreated: (GoogleMapController controller) {
      controller.animateCamera(
        CameraUpdate.newCameraPosition(CameraPosition(
           target: LatLng(snapshot.data['lat'],
            snapshot.data['lng']),
             zoom: 12)));
      setState((){});
}

Hope this helps! ;)

VXGamez
  • 59
  • 3
  • Thanks for your comment. I just tried this one but it is not updated the camera position. Marker positions are updating according to new coordinates but camera position just stuck at initial position – Aiwatech Developer Jun 13 '22 at 13:54
  • When it comes to the markers, how are you declaring them? I'd recommend you create a global Set markers; and initialize it to {} and then have whatever function you've created to create the markers add them to the set by doing markers.add(Marker(markerId ... , position: ..., icon: ....)) and on the map widget reference this variable by doing markers: Set.from(markers) Let's see if this works ;) – VXGamez Jun 13 '22 at 14:04