2

How to make custom view for marker or how to make custom layout. like this... enter image description here and please look at screenshot:

enter image description here

4

3 回答 3

5

使用布局的谷歌地图自定义标记

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

从 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;
}
于 2014-07-10T05:52:54.123 回答
4

我可能有点晚了,但我会为其他面临/面临类似问题的人发布解决方案。所以基本上你必须做的(至少对于你正在寻找的解决方案,即在类似框的背景上施加的自定义图像)是在画布的帮助下将 customImage 强加在背景框上。使用此实现,您可以有效地从画布创建 BitmapDrawable,然后您可以将其指定为“Overlay”/“ItemizedOverlay”的标记。此外,请不要为每个叠加层创建一个 ImageView,因为如果您必须同时处理数千个这样的 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);

}
于 2012-07-09T19:52:41.040 回答
3

我还没有编写 android 代码,所以这主要基于这里的最后一个代码块:http ://code.google.com/apis/maps/articles/android_v3.html

它最终只是一个常规地图实现,因此您将执行以下操作以将标记添加到现有地图:

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});
于 2011-10-19T15:17:45.640 回答