【问题标题】:How to trigger custom info window programatically in Android如何在 Android 中以编程方式触发自定义信息窗口
【发布时间】:2013-12-26 02:50:23
【问题描述】:

我使用自定义窗口信息,但是,调用 marker.showInfoWindow();始终呈现默认窗口信息,而如果用户单击标记,则可以毫无问题地呈现自定义窗口信息。我可以以编程方式打开我的自定义窗口信息吗?

我的情况是,当在地图上绘制标记时,一个特定的标记应该显示其窗口信息(因此没有用户交互),但最好是自定义的,如我的 CustomWindowInfoAdapter 类中定义的那样。


编辑,很乐意删除这个问题,只是我笨拙,但也许还有更多像我这样的人。无论如何,我的问题是我在我的方法 resourceRepresentationsNearBy() 中添加适配器之前调用了 showInfoWindow,所以显然只有默认的信息窗口是可能的。所以我的错误代码:

    private void setUpMap() {
    ...

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

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

            ...
   }

其中,更正的代码是:

    private void setUpMap() {
    ...

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

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

            ...
   }

【问题讨论】:

  • 如果您不显示代码的相关部分,我们就无法判断您的代码中的错误。
  • 实际上,只要自信地暗示它与代码有关,您就帮助了我。我的代码很简单,所以我没有计算那里的错误。但是,我在绘制标记时,首先是在添加窗口信息适配器之后。由于我在绘制标记方法中调用了 showInfoWindow,因此只能打开默认信息窗口。我想我应该接受答案,道德存在,总是发布代码,或者更好的是,更仔细地检查。谢谢Maciej
  • 很高兴我能帮上忙。如果您以后想帮助读者,请编辑问题以放置错误代码,添加答案以显示错误并最终接受。

标签: android google-maps-markers google-maps-android-api-2


【解决方案1】:

试试这个:

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...    
}

}

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 2018-01-06
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多