如何使用Android Map API v2创建自定义形状的位图标记 [英] How to create a custom-shaped bitmap marker with Android map API v2

查看:60
本文介绍了如何使用Android Map API v2创建自定义形状的位图标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用Google Map API v2的Android应用程序.我需要使用自定义标记在地图上显示用户位置.

I am developing an Android Application where I'm using Google Map API v2. I need to show the user location on a map with custom markers.

每个标记都会显示来自URL的用户图片.必须以异步模式从服务器下载映像.有关示例,请参见所附的屏幕截图.

Each marker will show the picture of the user from an URL. The image must be downloaded in asynchronous mode from the server. See the attached screenshot for an example.

如何在标记中添加图像和自定义信息?

How do I add an image and custom information in the marker?

推荐答案

Google Maps API v2演示有一个MarkerDemoActivity类,您可以在其中查看如何将自定义图像设置为GoogleMap.

In the Google Maps API v2 Demo there is a MarkerDemoActivity class in which you can see how a custom Image is set to a GoogleMap.

// Uses a custom icon.
mSydney = mMap.addMarker(new MarkerOptions()
    .position(SYDNEY)
    .title("Sydney")
    .snippet("Population: 4,627,300")
    .icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow)));

因为这只是将标记替换为图像,所以您可能想使用Canvas绘制更复杂和更奇特的内容:

As this just replaces the marker with an image you might want to use a Canvas to draw more complex and fancier stuff:

Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp = Bitmap.createBitmap(80, 80, conf);
Canvas canvas1 = new Canvas(bmp);

// paint defines the text color, stroke width and size
Paint color = new Paint();
color.setTextSize(35);
color.setColor(Color.BLACK);

// modify canvas
canvas1.drawBitmap(BitmapFactory.decodeResource(getResources(),
    R.drawable.user_picture_image), 0,0, color);
canvas1.drawText("User Name!", 30, 40, color);

// add marker to Map
mMap.addMarker(new MarkerOptions()
    .position(USER_POSITION)
    .icon(BitmapDescriptorFactory.fromBitmap(bmp))
    // Specifies the anchor to be at a particular point in the marker image.
    .anchor(0.5f, 1));

这会将画布canvas1绘制到GoogleMap mMap上.该代码应该(大部分)说明一切,那里有许多教程如何绘制Canvas.您可以从Android查看画布和可绘制对象开始开发人员页面.

This draws the Canvas canvas1 onto the GoogleMap mMap. The code should (mostly) speak for itself, there are many tutorials out there how to draw a Canvas. You can start by looking at the Canvas and Drawables from the Android Developer page.

现在您还想从URL下载图片.

Now you also want to download a picture from an URL.

URL url = new URL(user_image_url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();   
conn.setDoInput(true);   
conn.connect();     
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is); 

必须从后台线程下载图像(可以使用 AsyncTask Volley

You must download the image from an background thread (you could use AsyncTask or Volley or RxJava for that).

之后,您可以用下载的图像bmImg替换BitmapFactory.decodeResource(getResources(), R.drawable.user_picture_image).

After that you can replace the BitmapFactory.decodeResource(getResources(), R.drawable.user_picture_image) with your downloaded image bmImg.

这篇关于如何使用Android Map API v2创建自定义形状的位图标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆