9447

Show dynamically to InfoWindow Googlemap V2

Question:

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.<br /> Now I want to add an image on that InforWindow using ImageView.<br /> 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: <img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/cQ34U.jpg" data-original="https://i.stack.imgur.com/cQ34U.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

How can I display images on InfoWindows, any advices are great!

Answer1:

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 <a href="http://code.google.com/p/android-maps-extensions/" rel="nofollow">Android Maps Extensions</a>, 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.

Answer2:

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.

Recommend