简体   繁体   English

有没有办法在Google地图上修复标记的准确半径?

[英]Is there a way to fix accuracy radius of marker on Google Maps?

For our application we need to show current user's location with some radius. 对于我们的应用程序,我们需要以一定的半径显示当前用户的位置。 But when I enabled map.setMyLocationEnabled(true); 但是当我启用map.setMyLocationEnabled(true); we've noticed, that accuracy radius changes in very large range (eg from 10m to 50m, then 15m etc), and it may confuse our users. 我们注意到,精度半径在非常大的范围内变化(例如,从10m到50m,然后是15m等),这可能会使我们的用户感到困惑。 We've decided to fix radius of accuracy circle around marker to give it a different meaning. 我们已决定在标记周围固定精度圆的半径,以赋予其不同的含义。

Is there a way to fix accuracy radius, in addition to creating your own marker? 除了创建自己的标记之外,还有其他方法可以固定精度半径? I don't want create my own marker, because I think that my realization would be much worse than Google's. 我不想创建自己的标记,因为我认为自己的实现会比Google的实现差很多。

Thanks in advance. 提前致谢。

Using the My Location layer there is no way you can change the symbology (icon) of the current location. 使用“ 我的位置”图层 ,您无法更改当前位置的符号系统(图标)。

As a workaround, you can implement this functionality using Location Updates and drawing the location on the map. 解决方法是,您可以使用位置更新并在地图上绘制位置来实现此功能。

To do this, you can use this implementation of LocationListener that draws the current location and accuracy: 为此,您可以使用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) {

    }
}

Improvements to be made 有待改进

You could rotate your marker based on the direction of the movement using public MarkerOptions rotation (float rotation) https://developers.google.com/android/reference/com/google/android/gms/maps/model/MarkerOptions.html#rotation(float) 您可以使用public MarkerOptions rotation (float rotation)基于运动的方向旋转标记。https://developers.google.com/android/reference/com/google/android/gms/maps/model/MarkerOptions.html#旋转(浮动)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM