Multiple InfoWindowAdatper's

故事扮演 提交于 2019-12-06 01:58:43

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!