How to trigger custom info window programatically in Android

点点圈 提交于 2020-01-11 09:15:10

问题


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.



来源:https://stackoverflow.com/questions/20463101/how-to-trigger-custom-info-window-programatically-in-android

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