74

我正在使用这部分代码MapFragment在 Google Map Version 2 中添加一个标记。

MarkerOptions op = new MarkerOptions();
op.position(point)
    .title(Location_ArrayList.get(j).getCity_name())
    .snippet(Location_ArrayList.get(j).getVenue_name())
    .draggable(true);
m = map.addMarker(op); 
markers.add(m);

我想使用我的drawable中的不同图像。

4

5 回答 5

138

这就是您如何将 a 设置DrawableMarker.

BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.current_position_tennis_ball)

MarkerOptions markerOptions = new MarkerOptions().position(latLng)
         .title("Current Location")
         .snippet("Thinking of finding some thing...")
         .icon(icon);

mMarker = googleMap.addMarker(markerOptions);

VectorDrawablesXMLbasedDrawables适用于此。

于 2013-08-05T09:15:40.877 回答
88

@Lukas Novak 答案没有显示任何内容,因为您还必须在Drawable.
这适用于任何可绘制对象。这是一个完整的工作示例:

public void drawMarker() {
    Drawable circleDrawable = getResources().getDrawable(R.drawable.circle_shape);
    BitmapDescriptor markerIcon = getMarkerIconFromDrawable(circleDrawable);

    googleMap.addMarker(new MarkerOptions()
            .position(new LatLng(41.906991, 12.453360))
            .title("My Marker")
            .icon(markerIcon)
    );
}

private BitmapDescriptor getMarkerIconFromDrawable(Drawable drawable) {
    Canvas canvas = new Canvas();
    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    canvas.setBitmap(bitmap);
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    drawable.draw(canvas);
    return BitmapDescriptorFactory.fromBitmap(bitmap);
}


circle_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <size android:width="20dp" android:height="20dp"/>
    <solid android:color="#ff00ff"/>
</shape>
于 2016-02-23T10:10:34.790 回答
10

如果您以Drawable编程方式创建(因此您没有资源),您可以使用它:

Drawable d = ... // programatically create drawable
Canvas canvas = new Canvas();
Bitmap bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmap);
d.draw(canvas);
BitmapDescriptor bd = BitmapDescriptorFactory.fromBitmap(bitmap);

然后你有BitmapDescriptor,你可以传入MarkerOptions

于 2015-09-08T09:08:25.383 回答
5

如果您正在vector drawable使用此扩展程序:

fun  Context.bitmapDescriptorFromVector(vectorResId:Int): BitmapDescriptor {
    val vectorDrawable = ContextCompat.getDrawable(this, vectorResId)
    vectorDrawable!!.setBounds(0, 0, vectorDrawable.intrinsicWidth, vectorDrawable.intrinsicHeight)
    val bitmap = Bitmap.createBitmap(vectorDrawable.intrinsicWidth, vectorDrawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
    vectorDrawable.draw(Canvas(bitmap))
    return BitmapDescriptorFactory.fromBitmap(bitmap)
}

并设置为标记图标:

val markerOptions = MarkerOptions().position(latLng)
         .title("Current Location")
         .snippet("Thinking of finding some thing...")
         .icon(bitmapDescriptorFromVector("Your icon resource id"))// call extension here

mMarker = googleMap.addMarker(markerOptions)
于 2020-12-23T08:32:58.860 回答
2

基本上使用可绘制的位图。如果您确实有可绘制对象,请将其转换为位图。
这是 kotlin 中的代码示例。

private fun placeNewMarker(whereToPlaceYourMarker: LatLng){

    val pickupMarkerDrawable = resources.getDrawable(R.drawable.your_marker_drawable,null)


    _pickupMarker =   mMap?.addMarker(MarkerOptions()
            .position(whereToPlaceYourMarker)
            .draggable(true)
            .title("My Marker")
            .icon(BitmapDescriptorFactory.fromBitmap(pickupMarkerDrawable.toBitmap(pickupMarkerDrawable.intrinsicWidth, pickupMarkerDrawable.intrinsicHeight, null))))



}
于 2020-03-23T10:05:53.647 回答