大家好,我是 Android 编程的初学者,我正在尝试为我的 map 应用程序上的标记制作自定义信息窗口。我的 InfoWindows 必须显示动态图像(我用 Picasso 库下载并设置)不同的临时标记和一些文本字段,例如“POI”的名称、地址和以分钟为单位的距离、步行和开车。问题是 InfoWindowAdapter 是图像,所以我看到唯一的方法是强制重新加载标记信息窗口的显示。但是,如果我尝试(正如我在某些论坛以及 StackOverflow 上的其他问题中看到的那样),我的应用程序就会崩溃。下面我发布了我的代码和评论,可以帮助你和我的应用程序的屏幕。真的很感谢大家。
截图:
代码:
// ****** Custom InfoWindowAdapter ****** //
map.setInfoWindowAdapter(new InfoWindowAdapter() {
View v = getLayoutInflater().inflate(R.layout.custom_info_window, null);
@Override
public View getInfoWindow(Marker marker) { //As I've seen on the web, if the marker is null and it is showing infowindow, I do a refresh,
if (marker != null && marker.isInfoWindowShown()){ //but with the debug I've seen that it doesn't never enter in this IF statement and nothing happen when the image is loaded.
marker.hideInfoWindow();
marker.showInfoWindow();
}
return null;
}
@Override
public View getInfoContents(final Marker marker) {
BuildInfoMatrix req = new BuildInfoMatrix();
String nome = marker.getTitle();
String currentUrl = "";
int vuoto = -15;
try{
currentUrl=req.findImageUrl(nome); //from another class (BuildInfoMatrix) I retrieve the right image marker URL to display
}catch(Exception e){
currentUrl = "http://upload.wikimedia.org/wikipedia/commons/b/bb/XScreenSaver_simulating_Windows_9x_BSOD.png"; //If the currentURL is null (I haven't set any URL for the marker) it set an error image
}
if (currentUrl == null)
{
currentUrl = "http://upload.wikimedia.org/wikipedia/commons/b/bb/XScreenSaver_simulating_Windows_9x_BSOD.png";
}
ImageView image;
image = (ImageView) v.findViewById(R.id.image_nuvoletta); //image_nuvoletta is where it will be placed on the InfoWindowAdapter
Picasso.with(v.getContext()) //Here with Picasso I download the image and I set into
.load(currentUrl) //R.id.image_nuvoletta
.error(R.drawable.ic_launcher)
.resize(150, 110)
.into(image, new Callback(){
@Override
public void onSuccess(){
//I should reload here (when the image have been downloaded) the infoWindowAdapter but
//if I place here "marker.showInfoWindow()" the app crash; without, nothing happens.
}
@Override
public void onError(){
}
});
/***********************************/
最佳答案
我有同样的问题,在去任何地方都没有找到答案之后,我尝试使用 new Handler().postDelayed()
并且它真的对我有用。
@Override
public View getInfoWindow(Marker marker) {
// Left it empty
return null;
}
@Override
public View getInfoContents(final Marker marker) {
....
Picasso.with(v.getContext()) //Here with Picasso I download the image and I set into
.load(currentUrl) //R.id.image_nuvoletta
.error(R.drawable.ic_launcher)
.resize(150, 110)
.into(image, new Callback(){
@Override
public void onSuccess(){
new Handler().postDelayed(new Runnable() {
public void run() {
marker.showInfoWindow();
}
}, 500);
}
@Override
public void onError(){
new Handler().postDelayed(new Runnable() {
public void run() {
marker.showInfoWindow();
}
}, 500);
}
});
}
如果此代码段适合您,请不要忘记投票。
关于android - 如何在 Android Maps v2 api 的标记上强制重新加载 InfoWindow?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26462413/