I'm implementing android Mapview with Custom marker. I'm using picasso to load image into marker view. And when i launch the app at the first time, it shows me all the markers, but only one marker that has loaded from database with picasso, the other markers are not loaded from database, they only show me the default maps marker pin. But when i go to the previous activity and go back into MapsActivity, it shows me all the markers that loaded from database with picasso.
Here's my PicassoMarker class
public class PicassoMarker implements Target {
Marker mMarker;
PicassoMarker(Marker marker) {
mMarker = marker;
}
@Override
public int hashCode() {
return mMarker.hashCode();
}
@Override
public boolean equals(Object o) {
if(o instanceof PicassoMarker) {
Marker marker = ((PicassoMarker) o).mMarker;
return mMarker.equals(marker);
} else {
return false;
}
}
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
mMarker.setIcon(BitmapDescriptorFactory.fromBitmap(bitmap));
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
//mMarker.setIcon(BitmapDescriptorFactory.fromResource(R.mipmap.here));
}
}
Here's the method in MapsActivity
public void plotMarkers(ArrayList<MyMarker> markers) {
if(markers.size() > 0) {
for (MyMarker myMarker : markers)
{
markerOption = new MarkerOptions().position(new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude()));
location_marker = mMap.addMarker(markerOption);
target = new PicassoMarker(location_marker);
Picasso.with(MapsActivity.this).load(myMarker.getmIcon()).resize(84, 125).into(target);
mMarkersHashMap.put(location_marker, myMarker);
i = getIntent();
if(i.getBooleanExtra("maps", true)) {
buttonNavigasi.setVisibility(View.VISIBLE);
location_marker.setTitle(i.getStringExtra("nama"));
dest = new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(dest, 16));
}
else {
mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
}
}
}
}
What's going wrong here?
Thanks.
Okay, so I managed to reproduce what you are experiencing and found what was causing your problem. In the code you provided, notice this line in
MapsActivity
:I presumed that you are using a global single variable for
target
. I added some logs and managed to see that the only Marker that gets the image loaded usingPicasso
is the lastMarker
in the for loop.The reason is because, every time you enter the loop, the value of
target
changes to the newerPicassoMarker
that you have, making theonBitmapLoaded
of the previousPicassoMarker
you have useless, since it no longer has a target. :(So what I did is, I just added a
List<Target>
variable (make sure you don't forget to initialize it) to store the instances of thetarget
s. In the line where that I specified earlier, I just added the code to store the value of thetarget
to the list, like so:Tested it on my emulator and it loads the images to all the
Marker
s.Here is the Activity code I used to reproduce your error and then modified it to make it work: