问题
I'm developing a travel app. On Google map API V2 feature, I want each InfoWindow have an different image.
I've override WindowInfoAdapter for custom title and detail text.
public class MyInfoWindowAdapter implements InfoWindowAdapter {
private final View mView;
public MyInfoWindowAdapter(Activity activity) {
mView = activity.getLayoutInflater().inflate(R.layout.custom_info_window, null);
}
@Override
public View getInfoContents(Marker marker){
String title = marker.getTitle();
final String snippet = marker.getSnippet();
final TextView titleUi = (TextView) mView.findViewById(R.id.title);
// final View subtitleUi = mView.findViewById(R.id.subtitle);
final TextView snippetUi = ((TextView) mView.findViewById(R.id.snippet));
final TextView placeid = ((TextView) mView.findViewById(R.id.placeid));
final RatingBar voteUi = ((RatingBar) mView.findViewById(R.id.ratingBar1));
final ImageView imageUi = ((ImageView) mView.findViewById(R.id.imageView1));
titleUi.setText(title);
//other code
return mView;
}
@Override
public View getInfoWindow(Marker marker){
return null;
}
}
It work fine because It's String.
Now I want to add an image on that InforWindow using ImageView.
The problem appear because I used image from asserts and stream. It's something like this
InputStream ims = getSherlockActivity().getAssets().open("images/" + imageSubPath + "/" + splitedImages[i].toLowerCase());
Drawable iImages = Drawable.createFromStream(ims, null);
I can't put Drawable object to snippet as I did with String before. The result I want here:
How can I display images on InfoWindows, any advices are great!
回答1:
You would have to create additional data structure to hold your images, e.g.:
Map<Marker, Drawable> allMarkersMap = new HashMap<Marker, Drawable>();
after you add marker to GoogleMap
:
allMarkersMap.put(marker, iImages);
in getInfoContents
:
Drawable iImages = allMarkersMap.get(marker);
and add it to the ImageView
.
Another way would be to use Android Maps Extensions, which has methods like Marker.setData(Object)
and Object Marker.getData()
. This you can use to directly assign Drawable
to marker, just like you do with snippet.
回答2:
After three hours of scratching my head I solved this with postDelayed handler. The idea is: object, comtaining data for infoWindow, must have some Drawable field (image, for example). When you first time show infoWindow, some AsyncTash or another async process obtains picture and put it's deawable into image. You must set postDealyed runnable, which will check image value everu X millis and if it became not null, re-show infoWindow, else - set another postDelayed run. My part of getInfoContents code:
final One o=data.get(id);
View v=o.inflateMarkerInfo(this);
if (o.image==null) {
final Handler h=new Handler();
h.postDelayed(new Runnable() {
@Override
public void run() {
if (o.image!=null) m.showInfoWindow();
else h.postDelayed(this, 100);
}
}, 100);
}
return v;
Sure, you can return null on first call so your infoWindow will not show until image obtained, but it will cause some lag.
来源:https://stackoverflow.com/questions/16729264/show-dynamically-to-infowindow-googlemap-v2