将Google地图锁定在用户位置

编程入门 行业动态 更新时间:2024-10-27 05:31:13
本文介绍了将Google地图锁定在用户位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望我的应用程序中的Google地图始终完全以用户为中心,并随着他们当前位置的变化与他们一起移动。 (认为​​口袋妖怪去,地图实际上如何与用户移动)

我目前的最佳实现只是每次更改位置时使用动画更新相机位置,就像这样:

//根据新的latlng更新相机的位置,但保持放大,倾斜和方位相同 CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(new CameraPosition(latLng, googleMap.getCameraPosition()。zoom,MAX_TILT,googleMap.getCameraPosition()。bearing)); googleMap.animateCamera(cameraUpdate); googleMap.setLatLngBoundsForCameraTarget(toBounds(latLng,300));

然而,这会使相机的移动有点波动,并且落后于实际的用户位置标记,特别是如果用户正在快速移动。

有没有一种方法可以将Google地图相机的移动与用户的移动完全匹配?

解决方案

我不认为标记实际上是在Google地图上的Pokemon Go的情况下。如果您想在地图中心修复图像(或任何类型的视图),只需确保图像位于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 = @ mip映射/ 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); ... }

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

使用此链接: developer.android/training/location/receive-location-updates.html p>

但是将部分代码更改为:

public class MainActivity扩展ActionBarActivity实现 ConnectionCallbacks,OnConnectionFailedListener,LocationListener { ... @Override public void onLocationChanged(Location location){ mCurrentLocation = location; mLastUpdateTime = DateFormat.getTimeInstance()。format(new Date()); moveUser(位置位置); private void moveUser(Location location){ LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude()); mGoogleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng)); mCharacterImageView.animate(pretendThatYouAreMovingAnimation) [你可以在用户的​​图像中制作一个动画...就像左转/右转进行移动] } $ / code>

如果您想要在移动方向上旋转角色,则需要比较以前的Latlng与新的和旋转您的图像(或查看...或任何东西)指向移动的方向。

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

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

I want the google map in my application to always be perfectly centered on the user, and move with them as their current location changes. (Think pokemon go, how the map actually moves with the user)

My current best implementation simply updates the camera location with an animation every time the location is changed, like so:

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

This however makes the camera movement somewhat choppy and it lags behind the actual user location marker, especially if the user is moving quickly.

Is there a way to tie the movement of the google maps camera so it exactly matches the movement of the user?

解决方案

I dont think that the marker is actually on the GoogleMap in case of Pokemon Go. If you want to fix an image (or any kind of view) in the center of the map... Just make sure that the image is at the center of the map in your xml file.

Like this:

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

So now you have a marker in the center of the map. Ok, but you still don't have it synchronized with the user position... So let's solve this.

I am going to assume that you don't have problems to start your map. So, do that, i'll wait. Done? Ok. Map set.

Just don't forget to disable drags in your map:

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

Let's receive the user location and move the map.

Using this link:

developer.android/training/location/receive-location-updates.html

But change some part of the code to this:

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

If you want to rotate your character in the movement direction, you will need to compare the previous Latlng with the new one and rotate your image (or view... or anything) to point to the moving direction.

If you need more info, maybe this repo could help you: CurrentCenterPositionMap

(The repo don't do what you want... it only uses the same concept of my explanation.)

更多推荐

将Google地图锁定在用户位置

本文发布于:2023-06-04 19:06:06,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位置   地图   用户   Google

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!