0

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.

mvasco
  • 4,965
  • 7
  • 59
  • 120
  • not the best solution but you can try this link https://stackoverflow.com/a/23237766/1548824 – akhilesh0707 Nov 10 '17 at 06:46
  • another solution is you can use `markeroption.setTag(yourvalue)` and get the value inside getInfoWindow `agr0.getTag();` this will the turn the value inside your custom infowindow – akhilesh0707 Nov 10 '17 at 06:50
  • @akhilesh0707, the option setTag is not available for markeroption.... – mvasco Nov 10 '17 at 06:53
  • please ref this link https://stackoverflow.com/questions/16997130/adding-custom-property-to-marker-google-map-android-api-v2 – akhilesh0707 Nov 10 '17 at 06:54
  • @mvasco if you want to get the icon you have two choices. first either save your markers in hash map or something and then get from there and second is use markeroption.getIcon() to get icon. But I think getIcon method is only available in web api of google maps. – Umair Nov 10 '17 at 06:55
  • @mvasco where did you create your `markerOptions` object? – akhilesh0707 Nov 10 '17 at 06:57
  • @akhilesh0707, this way: MarkerOptions markerOptions = new MarkerOptions(); – mvasco Nov 10 '17 at 07:02
  • @mvasco what is your play-services lib version? – akhilesh0707 Nov 10 '17 at 07:17

2 Answers2

1

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

akhilesh0707
  • 6,709
  • 5
  • 44
  • 51
1

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

Arnav Rao
  • 6,692
  • 2
  • 34
  • 31