How to trigger custom info window programatically

2019-02-24 03:15发布

问题:

I use custom window info, however, calling marker.showInfoWindow(); always renders default window info, whilst if the user clicks on the marker, custom window info are rendered without problem. Can I programatically open my custom window info?

My case is that when the markers are drawn on the map, one particular marker should show its window info (so no user interaction), but preferably a custom one, as defined in my CustomWindowInfoAdapter class.


EDIT, would gladly delete this question, it was just me being clumsy, but maybe there's more guys like me out there. Anyways, my problem was that I was calling showInfoWindow before adding the adapter in my method resourceRepresentationsNearBy(), so obviously only the default info window was ever possible. So my erroneus code:

    private void setUpMap() {
    ...

    // Add search result markers to the map.
    resourceRepresentationsNearBy();

    // Setting up custom info window
    mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());

            ...
   }

where as, corrected code is:

    private void setUpMap() {
    ...

    // Setting up custom info window
    mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());

    // Add search result markers to the map.
    resourceRepresentationsNearBy();

            ...
   }

回答1:

Try this:

private static GoogleMap _googleMap = null;
...


public static void initialize_google_map()
{
    _googleMap = ((MapFragment)Context.getFragmentManager().findFragmentById(R.id.map)).getMap(); 

    ...

    _googleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
        @Override
        public View getInfoWindow(Marker marker) {
            return build_info_window(marker);
        }
        @Override
        public View getInfoContents(Marker arg0) {return null;}
    });


}


private static View build_info_window(Marker marker)
{
    LayoutInflater inflater = (LayoutInflater)Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View custom_info_window = inflater.inflate(R.layout.custom_info_window, null);

    TextView tv_title = (TextView)custom_info_window.findViewById(R.id.tv_title);
    tv_title.setText("Title");

    etc... etc...    
}

}

Hope it helps.