在 Google Maps V2 Android 上根据用户方向旋转标记 [英] Rotate marker as per user direction on Google Maps V2 Android

查看:30
本文介绍了在 Google Maps V2 Android 上根据用户方向旋转标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据从加速度计接收到的轴承或传感器值旋转标记,以向用户显示他实际移动的位置.我已将标记图标和平面值设置为 true,但无法按要求工作.

I want to rotate marker as per bearing or sensor value received from Accelerometer to show the user where actually he is moving. I have set marker icon and flat value to true but its not working as required.

mCurrentLocationMarker.position(new LatLng(
                            LocationUtils.sLatitude, LocationUtils.sLongitude));
                    mCurrentLocationMarker.icon(icon);
                    mCurrentLocationMarker.flat(true);
                    mCurrentLocationMarker.rotation(LocationUtils.sBearing);

                    if (currentMarker != null) {
                        currentMarker.setPosition(new LatLng(
                                LocationUtils.sLatitude,
                                LocationUtils.sLongitude));
                    } else {
                        currentMarker = mGoogleMap
                                .addMarker(mCurrentLocationMarker);
                    }
                    animateCameraTo(true);

我用这个 作为标记.

I have used this as marker.

我不知道为什么它没有按照用户的方向旋转.如果有人有任何想法,请帮助我哪里出错了.

I don't know why its not rotating as per user's direction. If anyone has any idea please kindly help me where i am making mistake.

LocationUtils.sBearing 是我从 onLocationChanged 或加速度计收到的 Bearing 值.

LocationUtils.sBearing is the value of Bearing which i received from onLocationChanged or accelerometer.

基本上我想让我的标记与谷歌地图标记相同,它向用户显示他们正在移动或转向的方向.

推荐答案

这是一个老问题,从那时起 API 似乎发生了变化.

This is an old question and it appears the API has changed since then.

我假设您能够承受设备.如果不是这里是 方便的教程.

I'm assuming you are able to get the devices bearing. If not here is a handy tutorial.

首先要创建一个标记,我们可以使用它来进行更新.

First thing is to create a marker we can use for bearing updates.

private Marker marker;

// Create this marker only once; probably in your onMapReady() method
marker = mGoogleMap.addMarker(new MarkerOptions()
        .position(new LatLng(myLatitude, myLongitude))
        .flat(true));

注意 .flat(true) 部分.确保我们的标记北对齐,这样即使用户旋转地图,我们的方位也能正常工作.

Note the .flat(true) portion. The ensures our marker is north aligned so that our bearings will work correctly even if the user rotates the map.

现在,当您获得方位更新时,您可以执行以下操作

Now when you get your bearing updates you can do the following

marker.setRotation(bearing);
// or if following the linked tutorial
// marker.setRotation((float) azimuth);

这假设您的标记图标在顶部具有向前的方向.如果您的标记像图中那样旋转,则必须先调整方位以进行补偿,然后再将其设置到标记上.只需一个简单的 setRotation(bearing - 45) 即可.

This assumes your marker icon has the forward direction at the top. If your marker is rotated like the one pictured, you will have to adjust the bearing to compensate before setting it to the marker. Just a simple setRotation(bearing - 45) should do it.

这篇关于在 Google Maps V2 Android 上根据用户方向旋转标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆