Last active
November 7, 2020 20:04
-
-
Save lsurvila/06b1816a650730f5a94f to your computer and use it in GitHub Desktop.
Downloads/transforms images with Glide and puts them on Google Map as markers (images will be cached automatically).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private Bitmap defaultPin; | |
private GoogleMap googleMap; | |
private List<MapItem> items; | |
private void putItemsOnMap() { | |
for (MapItem item : items) { | |
// wrap google map marker with target, also set marker's pin with default pin bitmap, coordinates, etc, so it still be shown on map while real one is downloaded/transformed | |
MarkerTarget marker = new MarkerTarget(googleMap.addMarker(new MarkerOptions().position(new LatLng(item.getLatitude(), item.getLongitude())).title(item.getName()).icon(BitmapDescriptorFactory.fromBitmap(defaultPin)))); | |
Glide.with(this) | |
.load(item.getPinImageUrl()) | |
.asBitmap() | |
.transform(new OverlayTransformation(getContext(), defaultPin)) | |
.into(marker) | |
} | |
} | |
class MapItem { | |
private String pinImageUrl; | |
private double latitude; | |
private double longitude; | |
private String name; | |
public getPinImageUrl() { | |
return pinImageUrl; | |
} | |
public getLatitude() { | |
return latitude; | |
} | |
public getLongitude() { | |
return longitude; | |
} | |
public getName() { | |
return name; | |
} | |
} | |
class MarkerTarget extends SimpleTarget<Bitmap> { | |
private Marker marker; | |
public MarkerTarget(Marker marker) { | |
this.marker = marker; | |
} | |
@Override | |
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) { | |
marker.setIcon(BitmapDescriptorFactory.fromBitmap(resource)); | |
} | |
} | |
class OverlayTransformation extends BitmapTransformation { | |
private Bitmap bitmapToOverlayOn; | |
public OverlayTransformation(Context context, Bitmap bitmapToOverlayOn) { | |
super(context); | |
this.bitmapToOverlayOn = bitmapToOverlayOn; | |
} | |
@Override | |
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { | |
Bitmap transformed = Bitmap.createBitmap(bitmapToOverlayOn.getWidth(), bitmapToOverlayOn.getHeight(), bitmapToOverlayOn.getConfig()); | |
Canvas canvas = new Canvas(transformed); | |
canvas.drawBitmap(bitmapToOverlayOn, new Matrix(), null); | |
canvas.drawBitmap(toTransform, (bitmapToOverlayOn.getWidth() - toTransform.getWidth()) / 2, | |
(bitmapToOverlayOn.getHeight() - toTransform.getHeight()) / 2, null); | |
return transformed; | |
} | |
@Override | |
public String getId() { | |
return "OverlayTransformation"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment