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

查看:21
本文介绍了如何使用 Android 地图 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 Demo 有一个 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));

这会将 Canvas canvas1 绘制到 GoogleMap mMap 上.代码应该(大部分)不言自明,有很多教程如何绘制 Canvas.您可以首先查看 Android 中的 Canvas 和 Drawables开发者页面.

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

必须从后台线程下载图像(您可以使用 AsyncTaskVolleyRxJava.

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

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

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

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

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