Home » Android » How to get image in infoWindow on Google Maps with picasso – Android

How to get image in infoWindow on Google Maps with picasso – Android

Posted by: admin June 15, 2020 Leave a comment


I have created an app that implements a MapsView. And when i click the marker, it will show me the info window where the data is retrieved from the database by using picasso. It works fine, but the problem is that the image in the info window not shows me the image that i needed, it still shows me the placeholder image. But when i click the map and click again to the marker, it will shows me the image that i needed. But if i’m not clicking on the map (still clicking the marker), it still always shows me the placeholder marker. How do I display the image that i needed (the placeholder image will replace with the image that i needed) with a time?

Here’s my code

public void plotMarkers(ArrayList<MyMarker> markers) {
    if(markers.size() > 0) {
        for (MyMarker myMarker : markers)
            dest = new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude());
            markerOption = new MarkerOptions().position(dest);
            location_marker = mMap.addMarker(markerOption);
            Target target = new PicassoMarker(location_marker);

            ImageView image = new ImageView(this);
            int width = image.getDrawable().getIntrinsicWidth();
            int height = image.getDrawable().getIntrinsicHeight();

            Picasso.with(MapsActivity.this).load(myMarker.getmIcon()).resize(width, height).onlyScaleDown().into(target);
            mMarkersHashMap.put(location_marker, myMarker);

            i = getIntent();
            if(i.getBooleanExtra("maps", true)) {
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(dest, 16));
            else {
                mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());

public class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter
    public View getInfoWindow(Marker marker) {
        return null;

    public View getInfoContents(Marker marker) {
        View v  = getLayoutInflater().inflate(R.layout.info_windowlayout, null);

        MyMarker myMarker = mMarkersHashMap.get(marker);

        TextView markerLabel = (TextView) v.findViewById(R.id.marker_label);

        ImageView foto = (ImageView) v.findViewById(R.id.foto);

        TextView anotherLabel = (TextView) v.findViewById(R.id.another_label);
        anotherLabel.setText("Baca selengkapnya...");

        return v;
How to&Answers:

The info window is basically a bitmap, captured from the views that you populated. As a result, changes to those views — such as Picasso asynchronously updating an ImageView — will not update the info window.

One solution that works is to call showInfoWindow() on the Marker after Picasso has obtained and cached the image. For example, this sample app uses Picasso to populate info windows, and uses a Picasso Callback to call showInfoWindow():

  Copyright (c) 2013-2014 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_

package com.commonsware.android.mapsv2.imagepopups;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.HashMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.model.Marker;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;

class PopupAdapter implements InfoWindowAdapter {
  private View popup=null;
  private LayoutInflater inflater=null;
  private HashMap<String, Uri> images=null;
  private Context ctxt=null;
  private int iconWidth=-1;
  private int iconHeight=-1;
  private Marker lastMarker=null;

  PopupAdapter(Context ctxt, LayoutInflater inflater,
               HashMap<String, Uri> images) {


  public View getInfoWindow(Marker marker) {

  public View getInfoContents(Marker marker) {
    if (popup == null) {
      popup=inflater.inflate(R.layout.popup, null);

    if (lastMarker == null
        || !lastMarker.getId().equals(marker.getId())) {

      TextView tv=(TextView)popup.findViewById(R.id.title);


      Uri image=images.get(marker.getId());
      ImageView icon=(ImageView)popup.findViewById(R.id.icon);

      if (image == null) {
      else {
        Picasso.with(ctxt).load(image).resize(iconWidth, iconHeight)
               .into(icon, new MarkerCallback(marker));


  static class MarkerCallback implements Callback {
    Marker marker=null;

    MarkerCallback(Marker marker) {

    public void onError() {
      Log.e(getClass().getSimpleName(), "Error loading thumbnail!");

    public void onSuccess() {
      if (marker != null && marker.isInfoWindowShown()) {

Basically, if when Picasso gets the image, the info window is still open for the associated Marker, I call showInfoWindow(). The visual effect resembles normal Picasso behavior: a placeholder, followed by the real image replacing the placeholder.


kotlin version

            .into(avatarView, object : Callback {
                override fun onSuccess() {
                    if (marker != null && marker.isInfoWindowShown) {

                override fun onError(e: Exception?) {
                    Timber.e("Error loading avatar to info window!")