Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify the size of the icon on the Marker in Google Maps V2 Android

In may app i use Map from Google Maps V2 and in this map i am trying to add markers each Marker with an icon, but the marker is taking the size of the icon which is making the icon looks flue. How can i specify the size of the marker in dp so that i can control how it looks like on the map

like image 427
Mohammad Haidar Avatar asked Mar 01 '16 08:03

Mohammad Haidar


People also ask

How do I make a custom marker map?

Create Custom Maps using Google MapsClick on the “Your Places” option in the menu. Click on the “Maps” Tab in the top right. Click on the “CREATE MAP” link at the bottom of the menu. Once you are on the map creation page, click the marker icon to add a marker to the page.

How do I change the marker icon on Google Maps flutter?

First, create a method that handles the asset path and receives a size (this can be either the width, height, or both, but using only one will preserve ratio). Then, just add it to your map using the right descriptor: final Uint8List markerIcon = await getBytesFromAsset('assets/images/flutter.


3 Answers

Currently it's not possible to specify a marker size using MarkerOptions, so your only option is to rescale your Bitmap before setting it as your marker icon.

Creating the scaled Bitmap:

int height = 100;
int width = 100;
BitmapDrawable bitmapdraw = (BitmapDrawable)getResources().getDrawable(R.mipmap.marker);
Bitmap b = bitmapdraw.getBitmap();
Bitmap smallMarker = Bitmap.createScaledBitmap(b, width, height, false);

Using smallMarker as the marker icon:

map.addMarker(new MarkerOptions()
        .position(POSITION)
        .title("Your title")
        .icon(BitmapDescriptorFactory.fromBitmap(smallMarker))
);
like image 187
User Learning Avatar answered Oct 07 '22 17:10

User Learning


The accepted answer is outdated (Resources::getDrawable has been deprecated since API level 22). Here's an updated version:

int height = 100;
int width = 100;
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable. marker);
Bitmap smallMarker = Bitmap.createScaledBitmap(b, width, height, false);
BitmapDescriptor smallMarkerIcon = BitmapDescriptorFactory.fromBitmap(smallMarker);

and then apply it in MarkerOption

.icon(smallMarkerIcon)
like image 39
O-9 Avatar answered Oct 07 '22 17:10

O-9


Kotlin version I used 0- 9 answer and used it with kotlin

fun generateHomeMarker(context: Context): MarkerOptions {
    return MarkerOptions()
        .icon(BitmapDescriptorFactory.fromBitmap(generateSmallIcon(context)))
}

fun generateSmallIcon(context: Context): Bitmap {
    val height = 100
    val width = 100
    val bitmap = BitmapFactory.decodeResource(context.resources, R.drawable.logo)
    return Bitmap.createScaledBitmap(bitmap, width, height, false)
}
like image 8
Dagnogo Jean-François Avatar answered Oct 07 '22 17:10

Dagnogo Jean-François