Google映射波束以在android MapFragment中显示用户方向/标题

时间:2017-01-25 04:21:03

标签: android google-maps location

enter image description here

是否有可能在Android谷歌 MapFragment 中使用闪亮的蓝色光束而不是不显眼的箭头?此新功能适用于Android上的Google地图。如何在应用程序中实现它?请分享您的想法。

谢谢!

1 个答案:

答案 0 :(得分:0)

我假设您本质上是要用自己的方向指示器代替?如果您要查找完整的“到达目的地的路线”,则有already answers

This answer建议从覆盖LocationListener开始设置自定义图标(.icon(yourIcon)上的MarkerOptions):

public class MyLocationLayer implements LocationListener {
    private GoogleMap map;

    private BitmapDescriptor markerDescriptor;
    private boolean drawAccuracy = true;
    private int accuracyStrokeColor = Color.argb(255, 130, 182, 228);
    private int accuracyFillColor = Color.argb(100, 130, 182, 228);

    private Marker positionMarker;
    private Circle accuracyCircle;

    public MyLocationLayer(GoogleMap map, int markerResource) {
        this.map = map;

        markerDescriptor = BitmapDescriptorFactory.fromResource(markerResource);
    }

    public void setDrawAccuracy(boolean drawAccuracy) {
        this.drawAccuracy = drawAccuracy;
    }

    public void setAccuracyStrokeColor(int color) {
        this.accuracyStrokeColor = color;
    }

    public void setAccuracyFillColor(int color) {
        this.accuracyFillColor = color;
    }

    @Override
    public void onLocationChanged(Location location) {
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        float accuracy = location.getAccuracy();

        if (positionMarker != null) {
            positionMarker.remove();
        }
        final MarkerOptions positionMarkerOptions = new MarkerOptions()
                .position(new LatLng(latitude, longitude))
                .icon(markerDescriptor)
                .anchor(0.5f, 0.5f);
        positionMarker = map.addMarker(positionMarkerOptions);

        if (accuracyCircle != null) {
            accuracyCircle.remove();
        }
        if (drawAccuracy) {
            final CircleOptions accuracyCircleOptions = new CircleOptions()
                    .center(new LatLng(latitude, longitude))
                    .radius(accuracy)
                    .fillColor(accuracyFillColor)
                    .strokeColor(accuracyStrokeColor)
                    .strokeWidth(2.0f);
            accuracyCircle = map.addCircle(accuracyCircleOptions);
        }
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
}

然后,您可以在.rotation()之后添加new MarkerOptions()来设置设备的当前旋转角度。这可以通过在“片段/活动”上有一个参数(例如private Int rotation = 0)并进行更新来完成。

此更新将通过在片段/活动中实现SensorEventListener并在onSensorChanged(event: SensorEvent)内更新设备旋转的缓存来完成。

从加速度计和磁场获得旋转(方位角)的一种相当复杂的方法是on this answer以及一些替代方法。

请确保您注销相应的监听器!