高德地图上显示自定义View方法

项目中有一个需求,能够将人的头像显示在地图上,用以显示用户的位置,这类的地图应用有很多,最常见的就是微信实时位置共享效果。
微信实时位置共享
高德提供的demo里面只是有在地图上绘制图标,并没有说明这种效果如何实现,这里就把实现方式说明一下:
1.自定义布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@null">
    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_centerInParent="true"
        android:adjustViewBounds="true"
        android:background="@null"
        android:src="@drawable/ic_bubble" />

    <com.away.mother.view.CircularImage
        android:id="@+id/badge"
        android:layout_width="52dp"
        android:layout_height="52dp"
        android:layout_alignParentTop="true"
        android:layout_marginTop="10dp"
        android:layout_centerHorizontal="true"
        android:adjustViewBounds="true"
        android:background="@null"                 android:src="@drawable/friendactivity_personalportrait_default" />
</RelativeLayout>

这里用到了一个显示圆形头像的自定义控件,大家可以根据实际应用选择不同的控件。
2.代码实现:

public View getBubble(String userhead) {
View view = this.getLayoutInflater().inflate(R.layout.layout_bubble,
null);
ImageView iv_user = (ImageView) view.findViewById(R.id.badge);

String image = userhead;
Bitmap bm = imageLoader.getBitmapFromMemoryCache(image);
if (bm != null) {
iv_user.setImageBitmap(bm);
}
return view;
}

/**
* 绘制系统默认的1种marker背景图片
*/
public void drawMarkers(LatLng latlng, String loc, String userhead) {
//获取自定义View
View view = getBubble(true, loc, userhead);

//创建Marker对象
Marker marker = aMap.addMarker(new MarkerOptions().position(latlng)
.icon(BitmapDescriptorFactory.fromView(view)).draggable(true));
int dir = (int) (Math.random() * 360);
Marker markerloc = null;
//绘制方向小蓝点
markerloc = aMap.addMarker(new MarkerOptions()
.position(latlng)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_loc)).draggable(true)
.anchor(0.5f, 0.5f));
markerloc.setRotateAngle(dir);
changeCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(
latlng, 18, 30, 0)), this);
}

猜你喜欢

转载自blog.csdn.net/xinlingjun2007/article/details/46563421