Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

android - Change marker size in Google Maps API v2

I'm trying to port my app to the brand new Google Maps API v2, but can't find how to change the size of the marker (some of my markers are smaller than default).

In v1, I used a Drawable which I scaled with setBounds() before adding it to the map.

But now, in v2, I can't use a Drawable. I've to use MarkerOptions().icon(), which takes just a BitmapDescriptor (generated with a BitmapDescriptorFactory).

Looking at the reference, there doesn't seem to be any support for setting or changing the BitmapDescriptor size.

So, have I missed something, or is it just plain impossible to set the size for custom markers in this API version?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can first convert it into Bitmap and change its size and then use that bitmap in as a custom marker. For example I first created a method which accepts name of your image file in drawable folder, and width and height of marker you want to set.

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

Then call like this in setUpMap() method to create a new marker of required size.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...