我的 android 使用 Google map android API,InfoWindow 的图像在第一次单击时不显示,但在第二次单击时可以显示
我使用自定义 infoWindow
void setMapInfoWindow(){
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker arg0) {
return null;
}
@Override
public View getInfoContents(Marker arg0) {
View v = getLayoutInflater().inflate(R.layout.windowlayout, null);
final ImageView img = (ImageView)v.findViewById(R.id.imageView3);
//image
Picasso.with(context).load("http://imgurl").resize(140,
}
});
}
这是我的标记设置过程
void setMarkers(){
...
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject datas=jsonArray.getJSONObject(i);
MarkerOptions tmp=new MarkerOptions()
.title("name")
.alpha(0.6f)
.position(new LatLng(123,456));//replace LatLng with sample
marker=mMap.addMarker(tmp);
}
....
setMapInfoWindow();
}
完成标记的设置后,我调用 setMapInfoWindow()
函数。
它可以在我的智能手机上使用,但是当您第一次单击信息窗口时,它不会显示图像;但第二次点击时就会显示。
我测试了一些情况:
- 将网页图片替换为本 map 片,问题依旧。
- 将所有标记存储到ArrayList中,所有处理完成后,将所有标记设置为
showInfoWindow()
,然后将所有标记设置为hideInfoWindow()
。可以,但是有一个信息窗口无法关闭(最后一个)。 - 我正在尝试使用位图来获取图像,但它没有显示图像,我尝试了很多来自stackoverflow的方法。但在使用 Picasso 库时它可以工作。
谢谢
问题解决: 加载时,Google Web 服务的图像 URL 似乎会更改为另一个 URL。
示例:
https://maps.googleapis.com/maps/api/place/photo?photoreference=
Google 会将其更改为以下网址:
所以我将 bool 值 not_first_time_showing_info_window 更改为 int,并回调了 3 次
int not_first_time_showing_info_window=0;
//show image
try {
if(not_first_time_showing_info_window==2){
not_first_time_showing_info_window=0;
Picasso.with(HomeActivity.this).load("http://....").resize(600,400).into(img);
}
else if (not_first_time_showing_info_window==1) {
not_first_time_showing_info_window++;
Picasso.with(HomeActivity.this).load("http://....").resize(600, 400).into(img,new InfoWindowRefresher(marker));
}else if(not_first_time_showing_info_window==0){
not_first_time_showing_info_window++;
Picasso.with(HomeActivity.this).load("http://....").resize(600,400).into(img,new InfoWindowRefresher(marker));
}
} catch (Exception e) {
img.setImageDrawable(null);
}
最佳答案
首先,您可以创建一个自定义回调类来实现com.squareup.picasso.Callback
:
private class InfoWindowRefresher implements Callback {
private Marker markerToRefresh;
private InfoWindowRefresher(Marker markerToRefresh) {
this.markerToRefresh = markerToRefresh;
}
@Override
public void onSuccess() {
markerToRefresh.showInfoWindow();
}
@Override
public void onError() {}
}
其次,在您的 Activity 中声明一个 bool 变量:
boolean not_first_time_showing_info_window;
第三,实现public View getInfoContents(Markermarker)
方法:
@Override
public View getInfoContents(Marker marker) {
View v = getLayoutInflater().inflate(R.layout.custom_window, null);
ImageView image = (ImageView)v.findViewById(R.id.image_view);
if (not_first_time_showing_info_window) {
Picasso.with(MainActivity.this).load("image_URL.png").into(image);
} else {
not_first_time_showing_info_window = true;
Picasso.with(MainActivity.this).load("image_URL.png").into(image, new InfoWindowRefresher(marker));
}
return v;
}
您还可以访问this GitHub page完成实现。
关于android - 信息窗口的图像在第一次单击时不显示,但在第二次单击时可以显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30186982/