2015-04-02 131 views
0

我正在使用Google Maps V2。我试图在运行时使用来自服务器的图像和文本创建自定义标记。我想有一个像标记一样的气球,它会根据图像位图的大小以及文本的大小来展开。Android - 动态自定义地图标记

例如:

可以说我有一个像这样的气球像:

enter image description here

像这样的图片:

enter image description here

我所试图实现将这个图像放置在气球内部,图像上方有一些文字,例如:

enter image description here

如果可能的话,我怎么能做到这一点?

我有最终的图像将作为在谷歌地图标记选项,像这样的东西图标:

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

我特地到StackOverflow上的无果类似的解决方案。接受的答案将包含我正在努力构建的方法,或者是我可以自己实现解决方案的一个非常好的方向(但请查看我的方法的参数以及返回结果,如果需要调整这不是一个大问题),提前感谢!

回答

1

那么,你可以在canvas上绘制你的图像,相互重叠。这个功能可以做到这一点。

所以你只要把你的图像/文本放在正确的位置,并将它们创建为位图,然后按顺序覆盖它们。

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