【发布时间】:2014-10-18 16:53:44
【问题描述】:
我正在使用 Google Map 和 Google Places API 开发一个应用程序,假设我使用不同的标记填充地图,并且我在 a 中跟踪这些标记
Map<Marker, Place> places = new HashMap<Marker, Place>();
这是我的地方的班级:
public class Place {
String placeId;
String name;
public Place(String placeId, String name) {
this.placeId = placeId;
this.name = name;
}
}
我希望能够使用基于 placeId 参数获取的数据动态填充 InfoWindow,这是我在 InfoWindowAdapter 中所做的:
map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
View v = getLayoutInflater().inflate(R.layout.info_window_layout, null);
TextView placeName = (TextView)v.findViewById(R.id.info_window_place_name);
Place place = places.get(marker);
if (place != null) {
placeName.setText(place.name);
String photoUrl = "http://www.plopcontenido.com/wp/wp-content/themes/PlopTheme/img/logo.png";
new DownloadPlacePhoto(v, marker, placeName.toString()).execute(photoUrl);
}
return v;
}
});
private class DownloadPlacePhoto extends AsyncTask<String, Void, Bitmap> {
View v;
Marker marker;
String placeName;
public DownloadPlacePhoto(View v, Marker marker, String placeName) {
this.v = v;
this.marker = marker;
this.placeName = placeName;
}
@Override
protected Bitmap doInBackground(String... urls) {
Bitmap download;
try {
InputStream in = new URL(urls[0]).openStream();
download = BitmapFactory.decodeStream(in);
return download;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap download) {
if (download != null) {
ImageView placeImage = (ImageView)v.findViewById(R.id.info_window_place_photo);
placeImage.setImageBitmap(download);
placeImage.setContentDescription(this.placeName);
placeImage.setVisibility(View.VISIBLE);
if (this.marker.isInfoWindowShown()) {
this.marker.hideInfoWindow();
this.marker.showInfoWindow();
}
}
}
}
问题是 InfoWindow 是一个“快照”而不是实时表示(我完全理解为什么)是关联的 Asynctask 将在另一个线程中运行,因此在没有我获取的数据的情况下已经拍摄了快照。
我听到人们谈论观察者模式和其他人谈论“您需要在进入 getInfoWindow 函数之前存储您的数据,但由于 Google Places 的限制,我无法再执行两个请求(一个用于图片,另一个用于获取有关特定地点的更多数据)用于每个标记。
知道如何执行此操作吗?
【问题讨论】:
-
这可能会有所帮助,它是关于在创建后将图像添加到 InfoWindow,但它基本上是相同的问题:stackoverflow.com/questions/15503266/…
-
我已经看过这个帖子了,但我不是真正的Android大师,还在努力学习,作者对我来说有点太宽泛了。
标签: android google-maps google-places-api infowindow