简体   繁体   English

ANDROID:如何在Google地图上为标记制作自定义视图(布局)

[英]ANDROID: How make custom view(layout) for marker on Google Maps

How to make custom view for marker or how to make custom layout. 如何为标记制作自定义视图或如何进行自定义布局。 like this... 像这样... 在此输入图像描述 and please look at screenshot: 请看截图:

在此输入图像描述

I might be a bit late but I'll post a solution for others who have faced/are facing a similar issue. 我可能会有点迟,但我会为面临/正面临类似问题的其他人发布解决方案。 So basically what you have to do (at least for the solution you are seeking, ie a custom image imposed on a box-like background) is impose the customImage on the background box with the help of a canvas. 因此,基本上您必须做的事情(至少对于您正在寻求的解决方案,即在类似盒子的背景上强加的自定义图像)是在画布的帮助下将customImage强加于背景框上。 Using this implementation you can effectively create a BitmapDrawable from the canvas that you can then assign as a marker for your 'Overlay' / 'ItemizedOverlay'. 使用此实现,您可以从画布中有效地创建一个BitmapDrawable,然后您可以将其指定为“Overlay”/“ItemizedOverlay”的标记。 Also, please refrain from creating an ImageView for each overlay as this will utterly destroy your memory/ your app if you have to deal with thousands of such ImageViews simultaneously. 此外,请不要为每个叠加层创建一个ImageView,因为如果您必须同时处理数千个这样的ImageView,这将完全破坏您的内存/应用程序。 Instead, use BitmapDrawables that can be assigned to the overlays during their construction and don't consume nearly enough memory as an ImageView. 相反,使用可在构造过程中分配给叠加层的BitmapDrawables,并且不会消耗足够的内存作为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);

}

custom marker for Google Maps using a layout 使用布局的Google地图自定义标记

View markerView = ((LayoutInflater) getActivity()
                        .getSystemService(
                                getActivity().LAYOUT_INFLATER_SERVICE))
                        .inflate(R.layout.map_marker, null);

set marker 设置标记

 marker = map.addMarker(new MarkerOptions()
                                        .position(latLng)
                                        .title(strName)
                                        .snippet(strStatus)
                                        .icon(BitmapDescriptorFactory
                                                .fromBitmap(createDrawableFromView(
                                                        getActivity(),
                                                        markerView))));

Create Bitmap from Drawable 从Drawable创建位图

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

I don't code android yet, so this is based largely on the last code block here: http://code.google.com/apis/maps/articles/android_v3.html 我还没有编写Android代码,所以这主要基于最后一个代码块: http//code.google.com/apis/maps/articles/android_v3.html

Where it ends up being just a regular map implementation, so you'd do the following to add a marker to an existing map: 它最终只是一个常规的地图实现,因此您需要执行以下操作以向现有地图添加标记:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM