简体   繁体   English

Android-动态自定义地图标记

[英]Android - Dynamic Custom Map Marker

I am using Google Maps V2. 我正在使用Google Maps V2。 I am trying to create custom markers at run time with images and text that come from a server. 我正在尝试使用来自服务器的图像和文本在运行时创建自定义标记。 I would like to have a balloon like marker that would stretch accordingly to the size of the image bitmap with a text on top of it. 我想有一个像气球一样的标记,该标记可以根据图像位图的大小相应地拉伸,并在其顶部带有文本。

For example: 例如:

Lets say I have a balloon image like this: 可以说我有一个像这样的气球图像:

在此处输入图片说明

A image like this: 像这样的图像:

在此处输入图片说明

What I am trying to achieve would be to place this image inside the balloon with some text above the image, something like this: 我要实现的目标是将该图像放置在气球内,图像上方有一些文字,如下所示:

在此处输入图片说明

If it is possible, how can I achieve this? 如果有可能,我该如何实现?

I have to place the final image as a icon for the marker option in google maps, something like this: 我必须将最终图像放置为Google地图中标记选项的图标,如下所示:

// Resource ID from the drawable folder
int balloon_id = getResources().getInteger(R.drawable.balloon_image);

// A bitmap image that came from the server (I got this done)
Bitmap image_from_server;

// Some text that came from the server (I got this done too)
String some_text;

// A method that will return the final resulted image to be placed to the marker options
// What I need is to find out the work out of this method (This is where I am struggling
BitmapDescriptor result_image = some_method(balloon_id, image_from_server, some_text);

// Marker Options for the custom marker
MarkerOptions mo = new MarkerOptions()
                  .position(latlng)
                  .icon(result_image);

// Then I can have my marker
Marker my_marker = google_map.addMarker(mo);

I have looked into StackOverflow for similar solutions to no avail. 我已经研究了StackOverflow,以寻求类似的解决方案,但无济于事。 The accepted answer would contain the method that I am struggling to build or a very good direction to where I can achieve the solution on my own (but please take a look at the parameters of my method and what it returns, if I need to adjust that is not a big problem), thanks in advance! 可接受的答案将包含我正在努力构建的方法,或者一个很好的方向,说明可以自行解决的方法(但是如果需要调整,请查看我的方法的参数及其返回的内容)那不是什么大问题),在此先感谢!

Well, you can draw your images on canvas , overlaying each other. 好了,您可以在canvas上绘制图像,彼此重叠。 This function would do just that. 该功能可以做到这一点。

so you just put your images/text in the correct position and create them as bitmap, then overlay them in order. 因此,您只需将图像/文本放在正确的位置并将其创建为位图,然后按顺序覆盖即可。

private Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bmp1, new Matrix(), null);
    canvas.drawBitmap(bmp2, new Matrix(), null);
    return bmOverlay;
}

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

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