user8777561
user8777561

Reputation:

How to convert bitmapDescriptor to bitmap?

Feature I am trying to implement is: enlarge marker with specific icon if it is the closest one to user's selected area. I store all markers in one collection collectionOfMarkers() and findClosest() method returns marker that is closest to user's area.

// Get closes marker
        closestMarker= getClosestItem(collectionOfMarkers(),latLng);
        mClusterRenderer.getMarker(closestMarker).setIcon(closestMarker.getIcon());

To resize that one marker I would need to pass new Bitmap with specified width and height parameters, however it also would need Bitmap source and I only have bitmapDescriptor from doing closestMarker.getIcon(). Is there a way to convert BitmapDescriptor to Bitmap? I do not have icon name or resource path, as there are multiple icons that marker could represent from, thus, it has to be extracted from marker itself.

Upvotes: 2

Views: 5850

Answers (1)

Khemraj Sharma
Khemraj Sharma

Reputation: 58974

public Bitmap resizeMapIcons(String iconName,int width, int height){
    Bitmap imageBitmap = BitmapFactory.decodeResource(getResources(),getResources().getIdentifier(iconName, "drawable", getPackageName()));
    Bitmap resizedBitmap = Bitmap.createScaledBitmap(imageBitmap, width, height, false);
    return resizedBitmap;
}

Use it like

googleMap.addMarker(new MarkerOptions()
            .title("New Marker")
            .snippet("Check out this place.")
            .position(chelsea).icon(BitmapDescriptorFactory.fromBitmap(resizeMapIcons("image_name",100,100))));

Upvotes: 0

Related Questions