Android Google Maps API v2:获取我的承载位置

时间:2015-03-09 21:21:17

标签: android google-maps google-maps-android-api-2

我想强制相机的行为就像你使用导航一样,这意味着当你向左旋转90°时,相机会做同样的事情。

我有一张Google地图,其中显示了我的位置(作为蓝点)。

mGoogleMap.setMyLocationEnabled(true);

当我移动时,蓝点带有一个箭头,显示我当前的方位。我想得到这个结论。

我使用 FusedLocationApi 获取当前位置:

 currentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

下面的代码是将相机设置为当前位置的动画,没有方位。我可以添加轴承,但我不知道它的价值。

    @Override
    public void onLocationChanged(Location location) {

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

    }

你知道如何获得当前的位置方向吗?我无法找到任何适当的信息。我非常感谢你能给我的任何帮助。

谢谢

1 个答案:

答案 0 :(得分:6)

我认为您可以尝试使用getOrientation(R, orientation)(并调整为真北)到get the device rotation。您可以参考here

另外,使用CameraPosition班级here调整相机位置。

编辑:我甚至找到了更好的解决方案。在Location课程中使用getBearing()方法。

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