Wie können wir mit der vector asset -Datei ein Kartenmarkierungssymbol erreichen, so wie Google es auf folgende Weise programmgesteuert anzeigt:
Aktualisieren:
map.addMarker(new MarkerOptions()
.position(latLng)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.your_vector_asset))
.title(title);
dies funktioniert nicht, wenn Sie mit Vektorobjekten arbeiten. Der Hauptgrund, die Frage zu stellen. Der Fehler mit dem obigen Code:
Java.lang.IllegalArgumentException: Image konnte nicht decodiert werden. Das bereitgestellte Bild muss eine Bitmap sein.
Ich habe genau dieselbe Anforderung gesucht, und diese Frage hat mich anfangs glücklich gemacht, aber genauso wie @Shuddh war ich mit den gegebenen Antworten nicht zufrieden.
Um meine Geschichte kurz zu fassen, verwende ich folgenden Code für diese Anforderung:
private BitmapDescriptor bitmapDescriptorFromVector(Context context, @DrawableRes int vectorDrawableResourceId) {
Drawable background = ContextCompat.getDrawable(context, R.drawable.ic_map_pin_filled_blue_48dp);
background.setBounds(0, 0, background.getIntrinsicWidth(), background.getIntrinsicHeight());
Drawable vectorDrawable = ContextCompat.getDrawable(context, vectorDrawableResourceId);
vectorDrawable.setBounds(40, 20, vectorDrawable.getIntrinsicWidth() + 40, vectorDrawable.getIntrinsicHeight() + 20);
Bitmap bitmap = Bitmap.createBitmap(background.getIntrinsicWidth(), background.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
background.draw(canvas);
vectorDrawable.draw(canvas);
return BitmapDescriptorFactory.fromBitmap(bitmap);
}
und ein Anwendungsbeispiel:
.icon(bitmapDescriptorFromVector(this, R.drawable.ic_car_white_24dp));
Hinweis: Sie möchten möglicherweise eine andere Begrenzung für Ihre Vektoren verwenden. Meine Vektoren waren 24dp groß und ich habe ein 48dp-png-Bild (blauer Teil, der auch ein Vektor sein kann) als Hintergrund verwendet.
UPDATE: Hinzufügen eines Screenshots, wie er angefordert wurde.
Sie können diese Methode verwenden:
private BitmapDescriptor bitmapDescriptorFromVector(Context context, int vectorResId) {
Drawable vectorDrawable = ContextCompat.getDrawable(context, vectorResId);
vectorDrawable.setBounds(0, 0, vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight());
Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
vectorDrawable.draw(canvas);
return BitmapDescriptorFactory.fromBitmap(bitmap);
}
So wird Ihr Code aussehen:
map.addMarker(new MarkerOptions()
.position(latLng)
.icon(bitmapDescriptorFromVector(getActivity(), R.drawable.your_vector_asset))
.title(title);
Konvertieren Sie die Vektorressource in ein Bitmap-Objekt und verwenden Sie BitmapDescriptorFactory.fromBitmap(bitmap)
Bitmap bitmap = getBitmapFromVectorDrawable(getContext(),R.drawable.ic_pin);
BitmapDescriptor descriptor =BitmapDescriptorFactory.fromBitmap(bitmap);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.icon(descriptor);
Bitmapkonverter:
public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
Drawable drawable = AppCompatResources.getDrawable(context, drawableId)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Lollipop) {
drawable = (DrawableCompat.wrap(drawable)).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;
}
Möglicherweise ist das Spiel etwas spät, aber das funktioniert gut mit Google Maps v2:
public static BitmapDescriptor getBitmapFromVector(@NonNull Context context,
@DrawableRes int vectorResourceId,
@ColorInt int tintColor) {
Drawable vectorDrawable = ResourcesCompat.getDrawable(
context.getResources(), vectorResourceId, null);
if (vectorDrawable == null) {
Log.e(TAG, "Requested vector resource was not found");
return BitmapDescriptorFactory.defaultMarker();
}
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, tintColor);
vectorDrawable.draw(canvas);
return BitmapDescriptorFactory.fromBitmap(bitmap);
}
Initialisiert als:
locationMarkerIcon = LayoutUtils.getBitmapFromVector(ctx, R.drawable.ic_location_marker,
ContextCompat.getColor(ctx, R.color.marker_color));
Verwendungszweck:
googleMap.addMarker(MarkerOptions().icon(getMarkerIcon()).position(latLng));
Hinweis: getMarkerIcon()
gibt nur die initialisierte Nicht-Variable locationMarkerIcon
-Membervariable zurück.
Bildschirmfoto:
Wenn jemand in Kotlin sucht, ist hier die Methode für Sie:
private fun bitmapDescriptorFromVector(context: Context, vectorResId:Int):BitmapDescriptor {
var vectorDrawable = ContextCompat.getDrawable(context, vectorResId);
vectorDrawable!!.setBounds(0, 0, vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight());
var bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
var canvas = Canvas(bitmap);
vectorDrawable.draw(canvas);
return BitmapDescriptorFactory.fromBitmap(bitmap);
}
die obige Methode konvertiert das Vektorsymbol in einen Bitmap-Beschreiber
map.addMarker(new MarkerOptions()
.position(latLng)
.icon(bitmapDescriptorFromVector(getActivity(), R.drawable.your_vector_asset))
.title(title)
und dieses für das Setzen der Markierung für Ihre Karte danke für Leo Droidcoder von seiner Antwort nur ich habe es in Kotlin umgewandelt