【问题标题】:Is there a way to fix accuracy radius of marker on Google Maps?有没有办法修复谷歌地图上标记的准确半径?
【发布时间】:2016-05-19 19:52:18
【问题描述】:

对于我们的应用程序,我们需要以一定的半径显示当前用户的位置。但是当我启用map.setMyLocationEnabled(true); 时,我们注意到,精度半径在非常大的范围内变化(例如从 10m 到 50m,然后是 15m 等),这可能会使我们的用户感到困惑。我们决定固定标记周围的精确圆半径以赋予它不同的含义。

除了创建自己的标记之外,还有其他方法可以修复精度半径吗?我不想创建自己的标记,因为我认为我的实现会比 Google 的差很多。

提前致谢。

【问题讨论】:

标签: android google-maps


【解决方案1】:

使用My Location layer 无法更改当前位置的符号(图标)。

作为一种解决方法,您可以使用Location Updates 并在地图上绘制位置来实现此功能。

为此,您可以使用LocationListener 的这个实现来绘制当前位置和精度:

import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.Circle;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.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) {

    }
}

有待改进

您可以使用 public MarkerOptions rotation (float rotation) https://developers.google.com/android/reference/com/google/android/gms/maps/model/MarkerOptions.html#rotation(float) 根据移动方向旋转标记

【讨论】:

  • 谢谢!这是非常有用的答案。
猜你喜欢
  • 1970-01-01
  • 2018-06-05
  • 1970-01-01
  • 2014-06-29
  • 2018-04-13
  • 2018-06-16
  • 1970-01-01
  • 2012-05-03
  • 1970-01-01
相关资源
最近更新 更多