【问题标题】:Lock Google Maps on Users Location在用户位置上锁定 Google 地图
【发布时间】:2017-01-04 17:01:26
【问题描述】:

我希望我的应用程序中的谷歌地图始终以用户为中心,并随着他们当前位置的变化而移动。 (想想 pokemon go,地图实际上是如何随着用户移动的)

我目前最好的实现只是在每次更改位置时使用动画更新相机位置,如下所示:

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

然而,这使得相机移动有些不稳定,并且落后于实际的用户位置标记,尤其是在用户快速移动的情况下。

有没有办法绑定谷歌地图相机的运动,使其与用户的运动完全匹配?

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    对于 Pokemon Go,我认为标记实际上不在 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>
    

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

    我假设您在开始地图时没有问题。所以,就这样吧,我等着。完毕?行。地图集。

    别忘了禁用地图中的拖动功能:

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

    让我们接收用户位置并移动地图。

    使用此链接:

    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]
            } 
        } 
    

    如果你想在移动方向上旋转你的角色,你需要将之前的 Latlng 与新的 Latlng 进行比较,然后旋转你的图像(或视图......或任何东西)以指向移动方向。

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

    (repo 不会做你想做的事......它只使用了我解释的相同概念。)

    【讨论】:

    • 谢谢!效果很好。
    猜你喜欢
    • 2020-12-13
    • 2012-05-21
    • 2016-05-10
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多