Add a cache to your InfoWindowAdapter code and change its getInfoContents() method to check it:
googleMap.setInfoWindowAdapter(new InfoWindowAdapter() { LruCache<Marker, View> cache = new LruCache<Marker, View>(8); // added // no change to getInfoWindow() @SuppressWarnings("unchecked") @Override public View getInfoContents(Marker marker) { View v = cache.get(marker); if (v == null) { LayoutInflater inflater = (LayoutInflater) MapsDemo.this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = inflater.inflate(R.layout.custom_info, null); new LocateGeopoint(v, marker).execute(marker.getPosition()); cache.put(marker, v); } return v; } });
Adjust the code in the LocateGeopoint class by adding a reference to Marker and calling showInfoWindow() after updating TextView.
public class LocateGeopoint extends AsyncTask<LatLng, String, List<Address>> { // : Marker marker; // added public LocateGeopoint(View v, Marker marker) { // : this.marker = marker; // added } @Override protected void onPostExecute(List<Address> addresses) { super.onPostExecute(addresses); if(addresses!=null){ // : tv.setText(city); marker.showInfoWindow(); // added } } // no change to doInBackground() }
NOTE. This answer is actually just a quick and dirty implementation (I have not tested it, but I think that it should work) based on the answer from MaciejGórski on this question, please read the link provided in this answer for more details :)
source share