Android的地图API第2版自定义标记与ImageView的自定义、标记、地图、Android

2023-09-12 21:30:42 作者:污法污天

我开发一个Android的应用程序,我使用谷歌地图API第2版。我需要在使用自定义标记的地图显示用户位置。标记会显示用户的图片,将加载的URL。图片必须下载从服务器异步模式。请按照所附的屏幕。

I am developing an Android Application where i used Google map api v2 . And i need to show user location on map with custom marker. Marker will show picture of user that will load from URL. Image must be download in Asynchronous mode from server. Please follow the attached screen.

推荐答案

在谷歌地图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)));

由于这只是取代了标志与图像您可能需要使用一个画布来吸引更多的复杂和票友的东西:

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, 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 。在code应(主要)为自己说话,也有很多教程在那里如何绘制画布。您可以通过查看画布启动和可绘制从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.

现在你也想从网址下载一个图片,假设你知道网址,你可以得到它与这片code:

Now you also want to download a picture from an URL, assuming you know the URL you can get it with this piece of code:

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

之后,你可以替换 BitmapFactory.de codeResource(getResources(),R.drawable.user_picture_image) bmImg 。你也的必须从后台线程下载的图像(你可以使用的AsyncTask 为)。

After that you can replace the BitmapFactory.decodeResource(getResources(), R.drawable.user_picture_image) with the bmImg. You also must download the image from an background thread (you could use AsyncTask for that).

 
精彩推荐
图片推荐