简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM