GoogleMap InfoWindowAdapter下载线程数据并在完成后更新

时间:2014-04-22 19:36:20

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

我正在使用自定义InfoWindow适配器类来处理用户按下图标以显示某些信息时的标记:

mapView.setInfoWindowAdapter(new CustomMarkerInfoWindowAdapter(downloadService, getLayoutInflater(), markersList));

来自我的自定义InfoWindowAdapter的相关代码段:

@Override
public View getInfoContents(Marker marker) {

    final View currentView = inflater.inflate(R.layout.custommarker, null);
    final TextView textThing = (TextView) currentView.findViewById(R.id.markerInfoThing);
    textThing.setText("Downloading data...");

    new Thread() {
        public void run() {

            Thing thing = downloadService.getThing(thingId);

            // this doesn't work - and yes thing.getName() returns a valid string
            textThing.setText(thing.getName());
        }
    }.start();

    return currentView;
}   

setText返回并且InfoWindow上的TextView永远不会更新之后,才会发生对getInfoContents的第二次调用。我注意到notifyDataHasChanged中没有其他功能(例如GoogleMap.InfoWindowAdapter)可以覆盖,因此我想知道是否可以这样做?

1 个答案:

答案 0 :(得分:1)

getInfoContents似乎在下载数据之前返回View,因此TextView未更新。

您应先下载内容,然后在下载后更新InfoWindow。您可以使用AsyncTask完成此操作,并且可以在InfoWindow方法调用中显示onPostExecute,为此,您需要跟踪当前Marker

请参阅以下链接:

  1. How to image from url into InfoWindowAdapter android?
  2. I my using google mapV2 and i m downloading image from google place api and want to display in the popup
  3. How to check if an Google Maps InfoWindow is still displayed before updating it?
  4. this answer还有这种方法:

      

    你应该在标记上做Marker.showInfoWindow()   当您收到模型更新时,当前显示信息窗口。

         

    所以你需要做三件事:

         
        
    1. 创建模型,而不是将所有下载内容放入CustomMarkerInfoWindowAdapter
    2.   
    3. Marker
    4. 保存对markerShowingInfoWindow的引用(称之为getInfoContents(Marker marker))   
    5. 当型号通知您下载完成时,请致电

      if (markerShowingInfoWindow != null && markerShowingInfoWindow.isShowingInfoWindow()) 
      {
          markerShowingInfoWindow.showInfoWindow();
      }
      
    6.   

    希望这会对你有所帮助。