【问题标题】:rotate the marker with move direction随移动方向旋转标记
【发布时间】:2017-12-08 10:14:34
【问题描述】:

我正在开发带有谷歌地图的安卓应用程序。我试图让标记随着移动方向旋转(如谷歌地图中的导航),但所有尝试的代码都失败了。下面是代码。任何建议。注意(我从返回位置、速度和 bering 的方法中获取方位,我可以看到方位的浮动值在改变方向时发生变化。

                float bearing = setterandGetter.getBearing();
                double lat = setterandGetter.getLatitude();
                double lon = setterandGetter.getLongitude();
                LatLng currentPosition = new LatLng(latitudelongitude);

                googleMap.moveCamera(CameraUpdateFactory.newLatLng(currentPosition));

Marker marker = googleMap.addMarker(new MarkerOptions().position(currentPosition).icon(BitmapDescriptorFactory.fromBitmap(blueIcon)).flat(true).rotation(bearing));

【问题讨论】:

  • 你把这段代码放在onLocatioChanged()方法里了吗?
  • 是的,当位置改变时会调用map方法
  • 那么您是否在该方法中打印了日志以检查它是否正在调用?
  • 是的,正在调用
  • 好的。我已经发布了答案。请参考。

标签: android google-maps


【解决方案1】:

试试这个:

首先全局声明标记变量并在 onCreate 中使用以下行对其进行初始化:

marker = googleMap.addMarker(new MarkerOptions().position(currentPosition).icon(BitmapDescriptorFactory.fromBitmap(blueIcon)).flat(true).rotation(0));

现在在 onLocationChanged() 中添加以下代码:

map.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
       @Override
       public void onMyLocationChange(final Location location) {
       marker.setPosition(new LatLng(location.getLatitude(),location.getLongitude()))
       marker.setRotation(location.getBearing());
    }
});

【讨论】:

    【解决方案2】:

    使用map.clear() 清除地图,然后创建标记,因此每当您的位置更改时,旧标记将自动被清除并创建新标记,因此标记似乎随着位置移动 p>

    【讨论】:

    • 我想保留地图上所有以前的标记,因为我的应用程序显示路线已通过,因此我无法在定位后清除所有标记。
    猜你喜欢
    • 1970-01-01
    • 2017-01-11
    • 1970-01-01
    • 2021-04-12
    • 1970-01-01
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多