在用户位置锁定Google地图

Dpo*_*ogy 3 android google-maps

我希望我的应用程序中的谷歌地图始终完全以用户为中心,并随着当前位置的变化而随之移动.(想想口袋妖怪去,地图实际上如何与用户一起移动)

我目前的最佳实现只是每次更改位置时都会使用动画更新摄像机位置,如下所示:

            // update the location of the camera based on the new latlng, but keep the zoom, tilt and bearing the same
        CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(new CameraPosition(latLng,
                    googleMap.getCameraPosition().zoom, MAX_TILT, googleMap.getCameraPosition().bearing));
        googleMap.animateCamera(cameraUpdate);

        googleMap.setLatLngBoundsForCameraTarget(toBounds(latLng, 300));
Run Code Online (Sandbox Code Playgroud)

然而,这使得相机运动有些不稳定并且它落后于实际的用户位置标记,特别是如果用户快速移动.

有没有办法绑定谷歌地图相机的运动,以便它完全匹配用户的运动?

Lea*_*ira 5

在口袋妖怪围棋的情况下,我不认为该标记实际上在GoogleMap上.如果要在地图中心修复图像(或任何类型的视图)...只需确保图像位于xml文件中地图的中心.

像这样:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@mipmap/ic_self_position"/>

        <fragment
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.SupportMapFragment"/>

    </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

所以现在你在地图的中心有一个标记.好的,但你仍然没有将它与用户位置同步......所以让我们解决这个问题.

我将假设你没有问题来开始你的地图.所以,那样做,我会等.完成了吗?好.地图集.

只是不要忘记在地图中禁用拖动:

@Override
    public void onMapReady(GoogleMap googleMap) {
       googleMap.getUiSettings().setScrollGesturesEnabled(false);

       ...
}
Run Code Online (Sandbox Code Playgroud)

让我们收到用户位置并移动地图.

使用此链接:

https://developer.android.com/training/location/receive-location-updates.html

但是将代码的某些部分更改为:

    public class MainActivity extends ActionBarActivity implements 
            ConnectionCallbacks, OnConnectionFailedListener, LocationListener { 
        ... 
        @Override 
        public void onLocationChanged(Location location) { 
            mCurrentLocation = location; 
            mLastUpdateTime = DateFormat.getTimeInstance().format(new Date()); 
            moveUser(Location location); 
        } 

        private void moveUser(Location location) { 
            LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
            mGoogleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng)); 

    mCharacterImageView.animate(pretendThatYouAreMovingAnimation)
[you can make a animation in the image of the user... like turn left/right of make walk movements]
        } 
    } 
Run Code Online (Sandbox Code Playgroud)

如果要在移动方向上旋转角色,则需要将之前的Latlng与新角色进行比较并旋转图像(或视图...或任何内容)以指向移动方向.

如果您需要更多信息,也许这个回购可以帮助您:CurrentCenterPositionMap

(回购不做你想要的......它只使用我解释的相同概念.)