Question

I have a map in which I am creating different types of markers. I cannot assign an info window adapter to a marker (gee wouldn't that be nice), I can only assign on InfoWindowAdapter for the entire map (at least I think).

My problem is that I want to show a different type of info widow depending on what I clicked. Id the only way to set one InfoWindowAdapter that will handle creating the correct type of info window based on the marker that I am passed?

Am I missing something easy?

Was it helpful?

Solution

When you add a marker to the map, you are receiving back an ID, which uniquely identifies your marker.

You can create an instance of your InfoWindowAdapter immediately after you add the marker and put it in a map, which keeps the ID as key and your InfoWindowAdapter as value.

Marker marker = map.addMarker(options);
// Create your special infoWindowAdapter for this marker
// ...
adapterMap.put(marker.getId(), youSpecialInfoWindowAdapter);

In the one central InfoWindowAdapter, which you register at the map, you can just use the ID of the marker to get the specific InfoWindowAdapter and delegate to the methods of that. Access to the map can e.g. be provided in the constructor of the InfoWindowAdapter (to avoid global or static variables):

class CentralInfoWindowAdapter implements InfoWindowAdapter {
    Map<String, GoogleMap.InfoWindowAdapter> adapterMap;

    public CentralInfoWindowAdapter(
            Map<String, GoogleMap.InfoWindowAdapter> adapterMap) {
        this.adapterMap = adapterMap;
    }

    @Override
    public View getInfoContents(Marker marker) {
        InfoWindowAdapter adapter = adapterMap.get(marker.getId());
        return adapter.getInfoContents(marker);
    }

    @Override
    public View getInfoWindow(Marker marker) {
        InfoWindowAdapter adapter = adapterMap.get(marker.getId());
        return adapter.getInfoWindow(marker);
    }

}

You can vary this principle of course. If you have only a few different InfoWindowAdapters depending on the "type" of the marker, you may put an enumeration into the map, which identifies the type of your marker and lets you decide, which kind of real InfoWindowAdapter to use inside the central InfoWindowAdapter, or you may still put instances of your special InfoWindowAdapter into the map, but use the same instance for the same type of marker.

OTHER TIPS

if i am right you want to show a different window adapter on each marker?.. if yes you can add a tag on each marker then inside one of the two infowindow function either infowindow() or infocontents() checks the marker tag and add the appropriate layout.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top