在我的应用中,我使用了 Google Maps V2 中的地图,并且我试图在这个地图上添加一个带有图标的标记,但是标记会采取图标的大小,这使得图标看起来很占空间。 我该如何指定标记的大小为 dp ,以便我可以控制它在地图上的外观呢?
在我的应用中,我使用了 Google Maps V2 中的地图,并且我试图在这个地图上添加一个带有图标的标记,但是标记会采取图标的大小,这使得图标看起来很占空间。 我该如何指定标记的大小为 dp ,以便我可以控制它在地图上的外观呢?
目前无法使用MarkerOptions
指定标记大小,因此您唯一的选择是在将其设置为标记图标之前重新调整Bitmap
的大小。
创建调整后的 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);
使用 smallMarker
作为标记图标:
map.addMarker(new MarkerOptions()
.position(POSITION)
.title("Your title")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker))
);
被接受的答案已经过时(自API 22级别以来,Resources :: getDrawable已被弃用),以下是更新版本:
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);
然后将其应用于MarkerOption中
.icon(smallMarkerIcon)
Kotlin版本 我使用了0-9答案,并将其与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)
}
Drawable circleDrawable = getResources().getDrawable(R.mipmap.primarysplitter);
bitmapDescriptor = getMarkerIconFromDrawable(circleDrawable);
private BitmapDescriptor getMarkerIconFromDrawable(Drawable drawable) {
Canvas canvas = new Canvas();
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, (int)getResources().getDimension(R.dimen._30sdp), (int)getResources().getDimension(R.dimen._30sdp));
drawable.draw(canvas);
return BitmapDescriptorFactory.fromBitmap(bitmap);
}