Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Google Maps API v2: get my bearing location

I would like to force the camera to behave like you are using navigation, it means when you rotate 90° left, the camera does the same thing.

I have a Google Map where my location (as a blue dot) is shown.

mGoogleMap.setMyLocationEnabled(true);

When I am moving, blue dot is with an arrow which shows my current bearing. I would like to get this bearing.

I am getting my current location using FusedLocationApi:

 currentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

Code below is animating camera to current location, but without the bearing. I can add the bearing, but I don't know the value.

    @Override
    public void onLocationChanged(Location location) {

        LatLng latLng = new LatLng( location.getLatitude(), location.getLongitude() );
        mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

    }

Do you know how to get the current location direction? I was unable to find any proper information about that. I would greatly appreciate any help you can give me.

Thank you

like image 615
mrek Avatar asked Mar 09 '15 21:03

mrek


1 Answers

I think you can try to use getOrientation(R, orientation) (and adjust for true north) to get the device rotation. You can refer to here.

Also, use the CameraPosition class here to adjust camera position.

EDIT: I even found better solution. Use getBearing() method in Location class.

if (location.hasBearing()) {
            CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(latLng)             // Sets the center of the map to current location
                .zoom(15)                   // Sets the zoom
                .bearing(location.getBearing()) // Sets the orientation of the camera to east
                .tilt(0)                   // Sets the tilt of the camera to 0 degrees
                .build();                   // Creates a CameraPosition from the builder
            mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
        }
like image 98
bjiang Avatar answered Sep 18 '22 03:09

bjiang