Set Image from drawable as marker in Google Map version 2
Asked Answered
Z

5

80

I am using this part of code to add a marker in a MapFragment in 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);

I want to use different images from my drawable.

Zook answered 5/8, 2013 at 7:53 Comment(5)
Do BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.current_position_tennis_ball) and then op.icon(icon);Jumper
Mr.Babar thanx for Your Answer its good and worked for me...U post it as a answer and i will accept it... Thanx Again.Zook
@MuhammadBabar can we pass image at titleKubetz
@muhammad i have look but i unable to do that...but it can dun.....Kubetz
@muhammad i want to pass title in imageKubetz
J
145

This is how you can set a Drawable as a Marker.

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);

VectorDrawables and XML based Drawables do not work with this.

Jumper answered 5/8, 2013 at 9:15 Comment(4)
This is correct, although the use of the word "any" drawable is incorrect. This only allows you to set BitmapDrawables. You can't, for example, set a drawable from xml with this.Tracheid
Well, you can - you just need to paint it into a Canvas first (drawable.draw(canvas)), then dump the Canvas to a Bitmap.Ravine
Well guys i really don't have any idea about this right now! so let the up votes decide who is correct :)Jumper
Would have been a good question if it was about drawable that has been modified on code-behind, but it's currently a "press on autocomplete" question.Unveil
J
92

@Lukas Novak answer is not showing anything because you also have to set the bounds on Drawable.
This works for any drawable. Here is a fully working example:

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>
Joettajoette answered 23/2, 2016 at 10:10 Comment(3)
My first marker drawn bigger than the others, I use exactly the same code with you. What might be the reason ? It's independent from the image source I tried with others.Hygrometry
I don't know. Post some code: the drawable xml or the cose used to create the drawable if you create it programmatically.Joettajoette
@Joettajoette Can you post the code for the circle_shape.xml. For some reason circle.getIntrinsicHeight/width return 0 for meBellaude
L
10

If you have Drawable created programatically (so you have no resource for it), you can use this:

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);

Then you have BitmapDescriptor, which you can pass into MarkerOptions.

Lynnett answered 8/9, 2015 at 9:8 Comment(2)
This creates empty images for meWesthead
Please refer to the example posted by @Joettajoette for a fully working example of this approach. This answer isn't quite complete.Adjudication
E
5

If you are using vector drawable use this extension:

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)
}

And set to the marker icon:

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)
Economy answered 23/12, 2020 at 8:32 Comment(0)
F
3

Basically use bitmap insted of drawable. If you do have a drawable, convert it to bitmap.
Here is a code example in 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))))



}
Fleming answered 23/3, 2020 at 10:5 Comment(1)
Upvote for showing how to set a Vector Drawable as the marker iconYurik

© 2022 - 2024 — McMap. All rights reserved.