Android Google Maps API v2 - 如何更改标记图标

时间:2013-08-28 11:23:38

标签: android google-maps icons google-maps-markers

我正在尝试更改标记图标。 我从一个服务器目录中获取图像。

每次“位”结果为null时,我都会设置断点。当我运行应用程序时,我得到java.lang.NullPointerException

File file = new File("J:\\!!! DOCUMENTS\\!Outsourcing\\AppStore\\Benzinostancii\\Petrol\\logo.png");

Bitmap bit = BitmapFactory.decodeFile(String.valueOf(file));

double Dlat = lat.get(index);
double Dlon = lon.get(index);
String info = Arrayinfo.get(index);
String name = Arrayname.get(index);

LatLng coordinate = new LatLng(Dlat, Dlon);
map.addMarker(new MarkerOptions()
    .icon(BitmapDescriptorFactory.fromBitmap(bit))
    .position(coordinate)
    .title(info)
).setSnippet(name);

8 个答案:

答案 0 :(得分:70)

// latitude and longitude
double latitude = 17.385044;
double longitude = 78.486671;

// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps");

// Changing marker icon
marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.my_marker_icon)));

// adding marker
googleMap.addMarker(marker);

More Info

答案 1 :(得分:12)

这很简单:

new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.icon))

答案 2 :(得分:5)

在较新的Android版GoogleMaps中,您无法在.setIcon()对象上调用MarkerOptions。相反,您必须在地图上添加标记选项,这将为您提供Marker,然后可以在其上更改图标。

在Kotlin中,代码如下所示:

val markerOptions = MarkerOptions()
markerOptions.position(LatLng(40.419900, -111.880767))
val marker: Marker?  = googleMap?.addMarker(markerOptions)
val bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.marker_customer_symbol)
marker?.setIcon(bitmapDescriptor)

答案 3 :(得分:3)

使用.icon() 像这样添加

Marker marker = map.addMarker(new MarkerOptions().position(currentLocation)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_place_holder)));

请注意,不要使用矢量图像,如果要使用矢量,请使用以下代码

private BitmapDescriptor bitmapDescriptorFromVector(Context context, @DrawableRes  int vectorDrawableResourceId) {
    Drawable background = ContextCompat.getDrawable(context, R.drawable.ic_map_pin_filled_blue_48dp);
    background.setBounds(0, 0, background.getIntrinsicWidth(), background.getIntrinsicHeight());
    Drawable vectorDrawable = ContextCompat.getDrawable(context, vectorDrawableResourceId);
    vectorDrawable.setBounds(40, 20, vectorDrawable.getIntrinsicWidth() + 40, vectorDrawable.getIntrinsicHeight() + 20);
    Bitmap bitmap = Bitmap.createBitmap(background.getIntrinsicWidth(), background.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    background.draw(canvas);
    vectorDrawable.draw(canvas);
    return BitmapDescriptorFactory.fromBitmap(bitmap);
}

答案 4 :(得分:1)

对于Xamarin C#用户:

tappedMarker.Remove();
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.SetTitle(tappedMarker.Title);
markerOptions.SetPosition(tappedMarker.Position);

markerOptions.SetIcon(BitmapDescriptorFactory.DefaultMarker(BitmapDescriptorFactory.HueGreen));
tappedMarker = googleMap.AddMarker(markerOptions);

答案 5 :(得分:-1)

内部" onMapReady(GoogleMap googleMap)"

import android.databinding.BaseObservable;
import android.databinding.Bindable;

public class Item extends BaseObservable {
    private String name;
    private Boolean checked;
    @Bindable
    public String getName() {
        return this.name;
    }
    @Bindable
    public Boolean getChecked() {
        return this.checked;
    }
    public void setName(String name) {
        this.name = name;
        notifyPropertyChanged(BR.name);
    }
    public void setChecked(Boolean checked) {
        this.checked = checked;
        notifyPropertyChanged(BR.checked);
    }
}

答案 6 :(得分:-1)

      mMap = googleMap;
 LatLng sydney = new LatLng(Lat,Lng);
       mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)));
 mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney,12.0f));

答案 7 :(得分:-1)

尝试一下

  BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.location);
            LatLng bangalore = new LatLng(12.9716, 77.5946);

 MarkerOptions markerOptions = new MarkerOptions().position(bangalore)
          .title("Current Location")
            .snippet("hello").icon(icon);


 mMap.addMarker(markerOptions);