6

I'm trying to get the map rotation (bearing) but can't find it with the map controller.

Using currently google_maps_flutter: ^1.1.1 and flutter: 1.22

5 Answers5

3

You can do that by using this geolocator plugin and then you can get your position with:

Position position = await Geolocator.getCurrentPosition();

and then update map with:

controller.animateCamera(
    CameraUpdate.newCameraPosition(
      CameraPosition(
        target: LatLng(position.latitude, position.longitude),
        zoom: 16,
        bearing: position.heading,
      ),
    ),
  ),
Hrvoje Čukman
  • 421
  • 4
  • 12
  • I think you mean to get the user's heading with geolocator. But I intended to get only the map's camera heading in order to get different headings when the user rotate the map. – Rafael Moreira Queiroz Apr 23 '21 at 13:33
3

You can get the real time updated bearing by following steps:

listen to changes of the location using:

double mainBearing = 15.0;
location.onLocationChanged.listen((locationLib.LocationData cLoc) {
  //You can get the bearing of current location by using
  mainBearing =  cLoc.heading

  //Or you can do newCameraPoistion here with new bearing
   _controller.animateCamera(
      CameraUpdate.newCameraPosition(
        CameraPosition(
          target: LatLng(cLoc.latitude, cLoc.longitude),
          zoom: zoomCamera,
          bearing: cLoc.heading,
        ),
      ),
    );
}
2

Actually I found what I was looking for. There is the onCameMove callback on GoogleMap constructor and it receives current CameraPosition as its argument. It has the bearing, target, tilt and zoom fields.

This way I can be aware of changes to the map's bearing at anytime.

PS: This function is executed repeatedly while the user is moving the map and should not perform expensive operations.

1

Found it: CameraPosition has bearing:

 controller.animateCamera(
            CameraUpdate.newCameraPosition(
              CameraPosition(
                  bearing: location.heading.value,
                  target: LatLng(location.latitude ?? 51.4,
                      location.longitude ?? 4.4),
                  zoom: 18),
            ),
0

You can find it on the properties of CameraPosition function from your GoogleMaps library.

_googleMapController!.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
        target: LatLng(cLoc.latitude!, cLoc.longitude!),
        bearing: cLoc.heading!,
        tilt: 30,
        zoom: 18,
      )))

and you can also call this from location.onLocationChanged.listen((LocationData cLoc) {}

And that's it.

djose90
  • 718
  • 5
  • 10