2

I'm trying to use Picasso now for my Custom Marker's Images. I was previously using a Bitmap and setting individual Marker's InfoWindow Images with those Bitmaps with the help of a HashMap. But I like how Picasso will cache the Images for me and helps scale them etc.

public class HomeActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener, GoogleMap.OnMapLongClickListener, DetailsDialog.DialogListener, PictureDialog.FinishedMemorySaving {

private GoogleMap mMap;
private LocationManager locationManager;
private Location location;
private String provider, title, desc;
private LatLng latlng;
private Memory memory;
private FragmentManager fm;
private HashMap<String, Memory> markers;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    markers = new HashMap<>();

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    provider = locationManager.getBestProvider(new Criteria(), false);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }

    location = locationManager.getLastKnownLocation(provider);

    locationManager.requestLocationUpdates(provider, 400, 1, this);

    if(location != null){
        Log.i("App Info", "Location found");
    } else {
        Log.i("App Info", "Location not found");
    }

}

//.....OTHER METHODS WORKING DELETED NOT NEEDED FOR THIS.....

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
        @Override
        public View getInfoWindow(Marker marker) {
            return null;
        }

        @Override
        public View getInfoContents(Marker marker) {
            View view = getLayoutInflater().inflate(R.layout.marker_layout, null);

            Bitmap bit = null;

            ImageView markerImage = (ImageView)view.findViewById(R.id.markerImage);
            TextView markerTitle = (TextView)view.findViewById(R.id.markerTitle);
            TextView markerDate = (TextView)view.findViewById(R.id.markerDate);

            if(markers != null && markers.size() > 0){

    // ADDED THE CALLBACK ALONG WITH MARKER
                Picasso.with(getApplicationContext())
.load(markers.get(marker.getId()).getImageMem())
                        .centerInside()
                        .fit()
                        .into(markerImage, new MarkerCallback(marker));
                markerTitle.setText(markers.get(marker.getId()).getTitleMem());
                markerDate.setText(markers.get(marker.getId()).getFormatedDate());
            }

            return view;
        }
    });
}

public void setImages(String markerID, View view){
    Log.i("PATH TESTER 2", markers.get(markerID).getImageMem());

}

@Override
public void showMemory(Memory memory) {
    String markerID;
    Toast.makeText(getApplicationContext(), "The Minions have saved your Memory!", Toast.LENGTH_SHORT).show();

    markerID = mMap.addMarker(new MarkerOptions().title(memory.getTitleMem()).position(memory.getLocationMem())).getId();

    Log.i("PATH TESTER", memory.getImageMem());

    markers.put(markerID, memory);

    mMap.moveCamera(CameraUpdateFactory.newLatLng(memory.getLocationMem()));
}

public class MarkerCallback implements Callback {
    Marker marker=null;

    MarkerCallback(Marker marker) {
        this.marker=marker;
    }

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

    @Override
    public void onSuccess() {
        if (marker != null && marker.isInfoWindowShown()) {
            marker.hideInfoWindow();
            marker.showInfoWindow();
        }
    }
}
}

Is there something I'm not doing right? The path is coming through fine and is correct. I've also set an ImageView in a dialog with the same path and it worked. Thanks for any help given.

EDIT:

I now seem to be getting the Error coming up that results in the MarkerCallBack when ever I click on the marker. It doesn't seem to set the actual Image on the marker though :/. Hope someone can help!

SmiffyKmc
  • 801
  • 1
  • 16
  • 34
  • 4
    Picasso is asynchronous. However, `getInfoContents()` does not support that. Maps V2 does not use your widgets, beyond generating a `Bitmap` from them shortly after `getInfoContents()` returns. By the time Picasso loads the image, it is too late for that info window. If you use Picasso's `Callback` option, to get control when the image is ready, you can call `showInfoWindow()` on the `Marker` to get `getInfoContents()` called again, where you can apply your image. – CommonsWare May 03 '16 at 21:22
  • Thanks very much for the fast reply :). I tried giving it a go myself with a bit of research so I'm not coming back annoying you right away :P. What I tried thanks to your guidance was 1) Set up callback, 2) Within the onSuccess method I set showInfoWindow() to the marker reference in the method. But it still didn't fix it. Did I do it wrong? – SmiffyKmc May 03 '16 at 21:34
  • Take a look here: http://stackoverflow.com/questions/24528482/image-not-loading-from-url-in-custom-infowindow-using-picasso-image-loading-libr/28885430#28885430 And also here: http://stackoverflow.com/a/22043781/4409409 – Daniel Nugent May 03 '16 at 21:36
  • That should be sufficient. Picasso should be fast enough if the image is already in its cache. – CommonsWare May 03 '16 at 21:37
  • @DanielNugent thanks for the link. It was just really helpful finding out why it wasn't setting as it looked like it should of set. I'll follow the link and see how it goes. – SmiffyKmc May 03 '16 at 21:38
  • 1
    Okay I tried using the Custom CallBack idea from the first link, but it still didn't seem to work. I'd really like to not use the Bitmaps as after a few Markers being set I get the OOM exception. Just handy using an API which helps keep it at a minimum with it's caching you know :/ – SmiffyKmc May 03 '16 at 21:45

0 Answers0