ANDROID:如何在谷歌地图上定制标记视图(布局)
如何为标记创建自定义视图或如何创建自定义布局。这样地。。。请看截图:ANDROID:如何在谷歌地图上定制标记视图(布局),android,google-maps,android-layout,itemizedoverlay,android-view,Android,Google Maps,Android Layout,Itemizedoverlay,Android View,如何为标记创建自定义视图或如何创建自定义布局。这样地。。。请看截图: 我还没有编写android代码,所以这主要是基于这里的最后一个代码块: 它最终只是一个常规的映射实现,因此您需要执行以下操作向现有映射添加一个标记: var myIcon=new google.maps.MarkerImage('my_icon.png'); var point=new google.maps.LatLng(someLatitude,someLongitude); var marker = new google
我还没有编写android代码,所以这主要是基于这里的最后一个代码块: 它最终只是一个常规的映射实现,因此您需要执行以下操作向现有映射添加一个标记:
var myIcon=new google.maps.MarkerImage('my_icon.png');
var point=new google.maps.LatLng(someLatitude,someLongitude);
var marker = new google.maps.Marker({position: point,map: map, icon:mc});
我可能会晚一点,但我会为其他面临类似问题的人发布解决方案。因此,基本上你要做的是(至少对于你正在寻找的解决方案来说,也就是在一个像盒子一样的背景上加上一个自定义图像)在画布的帮助下在背景框上加上自定义图像。使用此实现,您可以有效地从画布创建一个BitmapDrawable,然后将其指定为“覆盖”/“ItemizeOverlay”的标记。此外,请不要为每个覆盖创建ImageView,因为如果您必须同时处理数千个这样的ImageView,这将彻底破坏您的内存/应用程序。取而代之的是,使用可以在构建覆盖时分配给覆盖的BitMapDrawable,并且不会像ImageView那样消耗足够的内存
public BitmapDrawable imageOnDrawable(int drawableBackground, Bitmap customImage)
{
//The following line is optional but I'd advise you to minimize the size of
//the size of the bitmap (using a thumbnail) in order to improve draw
//performance of the overlays (especially if you are creating a lot of overlays).
Bitmap customImageThumbnail = ThumbnailUtils.extractThumbnail(
customImage, 100, 100);
Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId);
bm = Bitmap.createScaledBitmap(bm, 112, 120, false);
Canvas canvas = new Canvas(bm);
canvas.drawBitmap(bm, 0, 0, null);
// The 6,6 in the below line refer to the offset of the customImage/Thumbnail
// from the top-left corner of the background box (or whatever you want to use
// as your background)
canvas.drawBitmap(customImageThumbnail, 6, 6, null);
return new BitmapDrawable(bm);
}
使用布局的谷歌地图自定义标记
View markerView = ((LayoutInflater) getActivity()
.getSystemService(
getActivity().LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.map_marker, null);
设置标记
marker = map.addMarker(new MarkerOptions()
.position(latLng)
.title(strName)
.snippet(strStatus)
.icon(BitmapDescriptorFactory
.fromBitmap(createDrawableFromView(
getActivity(),
markerView))));
从可绘制创建位图
public static Bitmap createDrawableFromView(Context context, View view) {
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay()
.getMetrics(displayMetrics);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
view.layout(0, 0, displayMetrics.widthPixels,
displayMetrics.heightPixels);
view.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(),
view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
那也许是这样的?包括
Drawable icon=getResources().getDrawable(someicon);icon.setBounds(0,0,icon.getIntrinsicWidth(),icon.getIntrinsicHeight());setMarker(图标)代码>@anticafe,你说得对。我注意到了一些Android的Java解决方案你有没有注意到wordpress的链接?嗨,机器人伍兹,现在我试试你的解决方案。“请给我一些时间。”机器人伍兹。这个链接不是我想要的。我需要做有背景的自定义标记。请看截图。