在 Google Maps API v2 中更改标记大小
我正在尝试将我的应用移植到全新的 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).
在 v1 中,我使用了一个 Drawable
,在将其添加到地图之前,我使用 setBounds()
对其进行了缩放.
In v1, I used a Drawable
which I scaled with setBounds()
before adding it to the map.
但是现在,在 v2 中,我不能使用 Drawable
.我必须使用 MarkerOptions().icon()
,它只需要一个 BitmapDescriptor
(用 BitmapDescriptorFactory
生成).
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
).
查看参考资料,似乎不支持设置或更改 BitmapDescriptor
大小.
Looking at the reference, there doesn't seem to be any support for setting or changing the BitmapDescriptor
size.
那么,我是不是遗漏了什么,还是根本不可能在这个 API 版本中设置自定义标记的大小?
So, have I missed something, or is it just plain impossible to set the size for custom markers in this API version?
您可以先将其转换为 Bitmap 并更改其大小,然后将该位图用作自定义标记.例如,我首先创建了一个方法,它接受 drawable 文件夹中图像文件的名称,以及要设置的标记的宽度和高度.
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;
}
然后在 setUpMap() 方法中像这样调用以创建所需大小的新标记.
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))));