Android Google Maps API v2: get my bearing location

橙三吉。 提交于 2019-12-19 03:38:11

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/28951974/android-google-maps-api-v2-get-my-bearing-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!