Rafael Moreira Queiroz
Rafael Moreira Queiroz

Reputation: 101

How do I get map rotation on google maps flutter?

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

Upvotes: 7

Views: 6274

Answers (5)

Qasim AlQari
Qasim AlQari

Reputation: 41

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,
        ),
      ),
    );
}

Upvotes: 4

djose90
djose90

Reputation: 719

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.

Upvotes: 0

Rafael Moreira Queiroz
Rafael Moreira Queiroz

Reputation: 101

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.

Upvotes: 3

Hrvoje Čukman
Hrvoje Čukman

Reputation: 447

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,
      ),
    ),
  ),

Upvotes: 3

Dirk Simons
Dirk Simons

Reputation: 11

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),
            ),

Upvotes: 1

Related Questions