在谷歌地图v2中更改自定义当前位置指示器 [英] Change custom current location indicator in google map v2

查看:222
本文介绍了在谷歌地图v2中更改自定义当前位置指示器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

地图截图



在google map v1中,我们可以使用逐项叠加来更改当前位置指示符,但我认为map v2与此不同。任何人都可以建议的方式来取代原来的指标图像??使用 google.com/maps/documentation/android-api/locationrel =noreferrer>我的位置图层,您无法更改当前位置的符号系统(图标)。



作为解决方法,您可以使用位置更新并在地图上绘制位置。



为此,您可以使用 LocationListener ,它绘制当前位置和准确性:

  import android.graphics.Color; 
导入android.location.Location;
导入android.location.LocationListener;
导入android.os.Bundle;
导入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;
导入com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
导入com.google.android.gms.maps.model.Marker;
导入com.google.android.gms.maps.model.MarkerOptions;

公共类MyLocationLayer实现LocationListener {
私人GoogleMap地图;

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

私人标记位置标记;
私人圆形精度圆形;

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();
}
最终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);


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

}

@Override
public void onProviderEnabled(String provider){

}

@Override
public void onProviderDisabled(String provider){


$ b

待改进



您可以使用 public MarkerOptions rotation(float rotation) https: //developers.google.com/android/reference/com/google/android/gms/maps/model/MarkerOptions.html#rotation(float)


map screenshot

In google map v1, we can change the current location indicator using itemized overlay, but i think map v2 is something apart from that. Can anyone suggest way to replace the original indicator image??

解决方案

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:

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)

这篇关于在谷歌地图v2中更改自定义当前位置指示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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