Skip to content

Instantly share code, notes, and snippets.

@nanopc
Last active May 12, 2019 09:11
Show Gist options
  • Save nanopc/7b693607e673c4bcafed701b5d3f54ab to your computer and use it in GitHub Desktop.
Save nanopc/7b693607e673c4bcafed701b5d3f54ab to your computer and use it in GitHub Desktop.
SVG-VectorDrawable as Maps Marker icon on Android
BitmapDescriptor markerIcon = vectorToBitmap(R.drawable.vectordrawableicon,
ContextCompat.getColor(getApplicationContext(),
R.color.marker));
mMap.addMarker(new MarkerOptions()
.icon(markerIcon)
.position(LatLng())
);
private BitmapDescriptor vectorToBitmap(@DrawableRes int id, @ColorInt int color) {
Drawable vectorDrawable = ResourcesCompat.getDrawable(getResources(), id, null);
assert vectorDrawable != null;
Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
DrawableCompat.setTint(vectorDrawable, color);
vectorDrawable.draw(canvas);
return BitmapDescriptorFactory.fromBitmap(bitmap);
}
// https://github.com/googlemaps/android-samples/blob/master/ApiDemos/app/src/main/java/com/example/mapdemo/MarkerDemoActivity.java#L347
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment