Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom marker in google maps in android

I want to add picture to marker in google maps. I want to show after clicked on marker picture and on right side of that picture some text. I create marker using this code:

Marker melbourne = map.addMarker(new MarkerOptions()
            .position(new LatLng(lblat,
                    lbLong))
            .title("Melbourne")
            .snippet("Population: 4,137,400")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

but in that solution I have only title and snippet. How do marker on my way?

like image 392
edi233 Avatar asked Feb 16 '23 14:02

edi233


2 Answers

You will have to inflate your own layout of the InfoWindow with an InfoWindowAdapter. See the documentation here.

Here's a little example (I have this as an inner class in my map Activity):

class MyInfoWindowAdapter implements InfoWindowAdapter {

    private final View v;

    MyInfoWindowAdapter() {
        v = getLayoutInflater().inflate(R.layout.infowindow,
                null);
    }

    @Override
    public View getInfoContents(Marker marker) {

        ImageView icon = (ImageView) v.findViewById(R.id.icon); 
        // set some bitmap to the imageview         

        return v;
    }

    @Override
    public View getInfoWindow(Marker marker) {
        // TODO Auto-generated method stub
        return null;
    }

}

And in your map Activity

gmap.setInfoWindowAdapter(new MyInfoWindowAdapter());

Good luck :)

like image 63
fweigl Avatar answered Feb 28 '23 10:02

fweigl


If you want to customize your marker by using an image, you can add this image to the drawable folder, then:

mMap.addMarker(new MarkerOptions()
.position(marq).icon(BitmapDescriptorFactory.fromResource((R.drawable.thenameofyourimage)))
like image 24
Luthermilla Ecole Avatar answered Feb 28 '23 10:02

Luthermilla Ecole