web-dev-qa-db-fra.com

Marqueur personnalisé dans Google Maps dans Android avec l'icône d'actif vectoriel

Comment pouvons-nous obtenir une icône de marqueur de carte avec le fichier vector asset, comme le montre Google, comme suit:

 map

Mettre à jour: 

map.addMarker(new MarkerOptions()
    .position(latLng)
    .icon(BitmapDescriptorFactory.fromResource(R.drawable.your_vector_asset))
    .title(title);

cela ne fonctionne pas avec les actifs vectoriels. La raison principale pour poser la question. L'erreur avec le code ci-dessus:

Java.lang.IllegalArgumentException: Impossible de décoder l'image. L'image fournie doit être un bitmap.

35
Shuddh

Je cherchais exactement la même chose et voir cette question me rendit heureux au début, mais comme @Shuddh, je n'étais pas satisfait des réponses fournies.

Pour que mon histoire soit courte, j'utilise le code suivant pour cette exigence:

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);
}

et un exemple d'utilisation:

.icon(bitmapDescriptorFromVector(this, R.drawable.ic_car_white_24dp));

Remarque: vous souhaiterez peut-être utiliser une délimitation différente pour vos vecteurs. Mes vecteurs avaient une taille de 24dp et j'avais utilisé une image png de 48dp (la partie bleue pouvant également être un vecteur) comme arrière-plan.

UPDATE: Ajout de la capture d'écran telle que demandée.

 Screenshot for end result

11
Sid Morad

Vous pouvez utiliser cette méthode:

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);
}

Donc, votre code ressemblera à:

map.addMarker(new MarkerOptions()
                .position(latLng)
                .icon(bitmapDescriptorFromVector(getActivity(), R.drawable.your_vector_asset))
                .title(title);
47
Leo Droidcoder

Cela pourrait être un peu tard pour le jeu mais cela fonctionne très bien avec 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);
}

Initialisé comme:

locationMarkerIcon = LayoutUtils.getBitmapFromVector(ctx, R.drawable.ic_location_marker,
                ContextCompat.getColor(ctx, R.color.marker_color));

Utilisation:

googleMap.addMarker(MarkerOptions().icon(getMarkerIcon()).position(latLng));

Remarque: getMarkerIcon() renvoie simplement la variable membre locationMarkerIcon non null initialisée.

Capture d'écran:

 enter image description here

0
prerak