How to use SVG icon in Google Maps addMarker (Markeroptions) API Android

Asked

Viewed 328 times

0

I am using the following Example code from the Google Maps v2 API for Android:

mMap.addMarker(new MarkerOptions()
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_bike))
                .anchor(0.0f, 1.0f) // Anchors the marker on the bottom left
                .position(new LatLng(41.889, -87.622)));

Worked very well for images .PNG, but in my APP I’m only using icons and images in .SVG own-created Vector Asset of Android Studio 3, and would like to keep this standard throughout the application, but fail to make addMarker accept my files. SVG...

I found the following question in Stackoverflow in English, but no answer worked for me:

Custom Marker in google maps in android with vector Asset icon

I need to make it clear that my need is to get a. svg file from the directories res/drawable and include as a method parameter icon() class Markeroptions() Google Maps API v2 p/ Android, please only respond if the solution is for this use.

If possible, do a test creating any icone. svg in Vector Asset of Android Studio and include this file as icon in Markeroptions, if it works there in your Maps, you have the solution that I seek.

Obs:

  • My SVG have been created correctly and are all functional, no problem in svg files.
  • I’m following this Google Maps Documentation Guide
  • Map Activity is functional with standard Bitmap icons (PNG).
  • The tests are being done on a (physical device) Samsung 7 Tablet" with Android version 4.4.4 SDK 19.

1 answer

1


Write a method to get a Bitmapdescriptor

private BitmapDescriptor getBitmapDescriptorFromVectorDrawable(int id) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        VectorDrawable vectorDrawable = (VectorDrawable) getDrawable(id);

        int h = vectorDrawable.getIntrinsicHeight();
        int w = vectorDrawable.getIntrinsicWidth();

        vectorDrawable.setBounds(0, 0, w, h);

        Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bm);
        vectorDrawable.draw(canvas);

        return BitmapDescriptorFactory.fromBitmap(bm);

    } else {
        return BitmapDescriptorFactory.fromResource(id);
    }
}

Change your code to use the method:

mMap.addMarker(new MarkerOptions()
                .icon(getBitmapDescriptorFromVectorDrawable(R.drawable.ic_bike))
                .anchor(0.0f, 1.0f) // Anchors the marker on the bottom left
                .position(new LatLng(41.889, -87.622)));

Note: The id passed to getBitmapDescriptorFromVectorDrawable() must be a Vectordrawable(SVG).

Source: Get Bitmapdescriptor from a Vectordrawable Resource for use as an icon on a Google Map Marker.

  • I am receiving the following error: VFY: Unable to resolve virtual method 18696: L com/example/myfirstapp/Viewmapsactivity;. getDrawable (I)Landroid/Graphics/drawable/Drawable;... E/Androidruntime(10017): at com.example.myfirstapp.ViewMapsActivity.onMapReady(Viewmapsactivity.java:87)... After that the APP stops, as well as the other examples I tested and quoted in the question.

  • The code has been tested in version 4.4.2 and 7.0. This error you mention seems to me to be due to the use of a method that does not exist in the version of the device, the method exists only in a higher version of the device. As this is another problem ask a new question, include the code of the map and the file Gradle.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.