Skip to content

Commit

Permalink
Google Maps: support non-bitmap drawables as marker icons
Browse files Browse the repository at this point in the history
  • Loading branch information
johan12345 committed Aug 11, 2020
1 parent 8962658 commit fa10f24
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,27 @@

package com.car2go.maps.google;

import android.content.Context;
import android.graphics.Bitmap;
import androidx.annotation.DrawableRes;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import com.car2go.maps.google.adapter.BitmapDescriptorAdapter;
import com.car2go.maps.model.BitmapDescriptor;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;

/**
* Creates instances of {@link com.car2go.maps.model.BitmapDescriptor}
*/
public class BitmapDescriptorFactory implements com.car2go.maps.BitmapDescriptorFactory {

private static final BitmapDescriptorFactory instance = new BitmapDescriptorFactory();
private final Context context;

private BitmapDescriptorFactory() {
}

public static BitmapDescriptorFactory getInstance() {
return instance;
public BitmapDescriptorFactory(Context context) {
this.context = context;
}

@Override
Expand All @@ -35,9 +38,40 @@ public BitmapDescriptor fromBitmap(Bitmap bitmap) {

@Override
public BitmapDescriptor fromResource(@DrawableRes int resourceId) {
return new BitmapDescriptorAdapter(
com.google.android.gms.maps.model.BitmapDescriptorFactory.fromResource(resourceId)
);
Drawable drawable = context.getResources().getDrawable(resourceId);
Bitmap bitmap = getBitmapFromDrawable(drawable);
return fromBitmap(bitmap);
}

/**
* Extract an underlying bitmap from a drawable
*
* @param sourceDrawable The source drawable
* @return The underlying bitmap
*/
@Nullable
private static Bitmap getBitmapFromDrawable(@Nullable Drawable sourceDrawable) {
if (sourceDrawable == null) {
return null;
}

if (sourceDrawable instanceof BitmapDrawable) {
return ((BitmapDrawable) sourceDrawable).getBitmap();
} else {
//copying drawable object to not manipulate on the same reference
Drawable.ConstantState constantState = sourceDrawable.getConstantState();
if (constantState == null) {
return null;
}
Drawable drawable = constantState.newDrawable().mutate();

Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ public class GoogleMapAdapter implements AnyMap {
private final GoogleMap map;
private final DrawableComponentFactory drawableComponentFactory;
private final Context context;
private final BitmapDescriptorFactory bitmapDescriptorFactory;

public GoogleMapAdapter(GoogleMap map, Context context) {
this.map = map;

drawableComponentFactory = new DrawableComponentFactory(map);
bitmapDescriptorFactory = new com.car2go.maps.google.BitmapDescriptorFactory(context);
this.context = context;
}

Expand Down Expand Up @@ -244,7 +246,7 @@ public void onUserLocationChanged(com.car2go.maps.model.LatLng location, float a

@Override
public BitmapDescriptorFactory getBitmapDescriptorFactory() {
return com.car2go.maps.google.BitmapDescriptorFactory.getInstance();
return bitmapDescriptorFactory;
}

@Override
Expand Down

0 comments on commit fa10f24

Please sign in to comment.