带有自定义标记的android

带有自定义标记的android

本文介绍了带有自定义标记的android Maps API v2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用自定义标记制作地图.在 API v2 中,我可以为标记设置图标、标题等.但我想在第一次出现时用标记显示标题.现在标题仅在我点击标记时显示.在 v1 中是叠加层,但在 v2 中我没有发现任何类似的东西.

I want to make maps with custom markers. In API v2 I can set icon, title, etc for markers. But I want to display title with marker at the first onset. Now title displays only when I taping the marker. In v1 was overlays, but in v2 I didn't found anything similar.

可能是我说的不够清楚.API 中的 Marker.showInfoWindow() 之类的东西仅适用于一个标记.我无法同时显示所有标记的信息窗口.无论如何,我需要显示所有标记的标题,而无需等待用户点击它.

Edited:Maybe I was not clear enough. Something like Marker.showInfoWindow() in API works only for one marker. I can't show info windows for all of my markers at the same time. Anyway I need to show titles for all of my markers, without waiting while user will tap on it.

推荐答案

我也偶然发现了这个问题.V2 API 前进了一步,后退了两步.谷歌,请在 Marker 或 GoogleMap 类上添加一个可覆盖的绘制"方法,以便我们可以自己自定义绘图.

I have also stumbled upon this problem. V2 API is a step forward, two steps back. Google, please add an overridable 'draw' method on the Marker or GoogleMap classes so we can customize the drawing ourselves.

一种可能的解决方案是动态生成位图并将其附加到标记上.即创建一个画布,插入标记位图,在标记旁边绘制文本.这涉及一些痛苦的计算(适当的画布大小,标记位图和文本彼此相邻).不幸的是,Marker 中没有 setIcon 方法,因此每次文本更改时,都必须创建一个新标记.如果地图上只有一个标记可能没问题,但是如果有几十个标记,这可能不可行.动态创建这些位图也可能存在内存问题.示例代码(仅包含文本):

A possible solution is to generate the bitmap on the fly and attach it to the marker. i.e. Create a canvas, insert the marker bitmap, draw the text next to the marker. This involves some painful calculations (the appropriate canvas size with the marker bitmap and the text next to each other). Unfortunately, there's no setIcon method in Marker, so every time the text changes, a new marker has to be created. It may be fine if you just have a marker on the map, but with dozens of markers, this may not be feasible. Also there may be memory issue on creating those bitmaps dynamically. A sample code (with just the text):

Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp = Bitmap.createBitmap(200, 50, conf);
Canvas canvas = new Canvas(bmp);

canvas.drawText("TEXT", 0, 50, paint); // paint defines the text color, stroke width, size
mMap.addMarker(new MarkerOptions()
                                .position(clickedPosition)
                                //.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker2))
                                .icon(BitmapDescriptorFactory.fromBitmap(bmp))
                                .anchor(0.5f, 1)
                                    );

希望 Google 会添加适当的方法,以便我们轻松完成此操作.该死,我真的很喜欢 V2 API 中新的地图旋转功能.

Hopefully, Google will add the appropriate methods so we can do this easily. Damn, I really like the new Map rotate feature in V2 API.

这篇关于带有自定义标记的android Maps API v2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 20:47