mvasco
mvasco

Reputation: 5101

Inserting map marker icon in marker infowindow

I am working on an Android app. I need to pass a map marker icon to its infowindow.

This is the method addMarker:

  private void addMarker(LatLng latlng, final String title, final String nombre_apellidos, final String datosreporte,final  String tiporeporte) {
        markerOptions.position(latlng);
        markerOptions.title(title);
        markerOptions.snippet(nombre_apellidos+": "+datosreporte);
        if (tiporeporte.equals("1")){
            markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.tipo_1));
        }
        else{
            markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_actihome));
        }


        mGoogleMap.addMarker(markerOptions).showInfoWindow();

        mGoogleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                Toast.makeText(getContext(), marker.getTitle(), Toast.LENGTH_SHORT).show();
            }
        });
    }

And this is how I create the infowindow:

 public View getInfoWindow(Marker arg0) {
                return null;
            }

            // Defines the contents of the InfoWindow
            @Override
            public View getInfoContents(Marker arg0) {
                View v = null;
                try {

                    // Getting view from the layout file info_window_layout
                    v = getLayoutInflater().inflate(R.layout.custominfowindow, null);

                    // Getting reference to the TextView to set latitude
                    TextView addressTxt = (TextView) v.findViewById(R.id.addressTxt);
                    TextView nameTxt = (TextView) v.findViewById(R.id.nameTxt);
                    ImageView icono = (ImageView) v.findViewById(R.id.iconoimagen);
                    addressTxt.setText(arg0.getTitle());
                    nameTxt.setText(arg0.getSnippet());



                } catch (Exception ev) {
                    System.out.print(ev.getMessage());
                }

                return v;
            }

I need to put the marker icon in the ImageView icono.

Thank you.

Upvotes: 1

Views: 835

Answers (2)

Arnav Rao
Arnav Rao

Reputation: 7002

The only object that is passed to methods of custom InfoWindowAdapter is Marker.

You can pass information from activity to your custom InfoWindowAdapter by setting tag on Marker object. You can add tag object to Marker by calling setTag method passing an object. And get the object from marker in InfoWindowAdapter and use the values.

You can create data transfer object which holds all the element that you want to show in info window on the map.

public class InfoWindowData {
    private String image;
    private String hotel;

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getHotel() {
        return hotel;
    }

    public void setHotel(String hotel) {
        this.hotel = hotel;
    }
}

In your activity

InfoWindowData data = new InfoWindowData();
data.setImage("blah");
data.setHotel("blah blah");

marker.setTag(data);

In custom info window class

@Override
    public View getInfoContents(Marker marker) {

        InfoWindowData data = (InfoWindowData) marker.getTag();
    ....

Complete example http://www.zoftino.com/google-maps-android-custom-info-window-example

Upvotes: 1

akhilesh0707
akhilesh0707

Reputation: 6899

Update your Google Maps Android API v2 version 9.4.0

com.google.android.gms:play-services:9.4.0

Instead of this

mGoogleMap.addMarker(markerOptions).showInfoWindow();

try this it will return a marker object and then you can the custom values setTag()

Marker marker=mGoogleMap.addmarker(new MarkerOptions(LatLang));
// Set your object value as tag
marker.setTag(yourValue)

// To get the value from marker

arg0.getTag();// Type cast to your object type;

For more Info ref this link

Upvotes: 1

Related Questions