先上效果图,摄像头和摄像头上的预警事件红点就是一个自定义布局作为地图的marker的图标。
camera_red.png (素材图)
官方提供的API参考手册:http://a.amap.com/lbs/static/unzip/Android_Map_Doc/index.html, maker图标设置如下截图所示
所谓自定义覆盖物,其实就是将一般的图片背景转成的BitmapDescriptor换成用自定义布局转换成BitmapDescriptor的方式而已,画好自定义view,经过动态加载转成BitmapDescriptor给icon()赋值即可,代码如下:
自定义view:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="60dp"
android:layout_height="60dp">
<ImageView
android:id="@+id/iv_maker_bg"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/camera_red"/>
<TextView
android:id="@+id/txt_count"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/round_count"
android:gravity="center"
android:text="3"
android:textColor="@color/white"
android:textSize="12sp" />
</RelativeLayout>
round_count.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 圆角 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android">
android:shape="rectangle">
<solid android:color="@color/red"></solid>
<corners android:radius="180dp"></corners>
</shape>
绘制核心函数代码,调用第一个就行:
//绘制覆盖物(摄像头list)
private void addOverlayCamera(AMap aMap, List<CameraInfoBean> points) {
clearCamera();
cameraMarkerList.clear();
for (CameraInfoBean bean: points) {
LatLng latLng = new LatLng(bean.getDeviceLat(),bean.getDeviceLng());
MarkerOptions markerOption = new MarkerOptions();
markerOption.position(latLng);
View view = getMarkerCountView(0,R.mipmap.camera_offline);
markerOption.icon(BitmapDescriptorFactory.fromView(view));
Marker marker = aMap.addMarker(markerOption);
marker.setObject(bean);
cameraMarkerList.add(marker);
}
}
public View getMarkerCountView(int count,int icon) {
View view = this.getLayoutInflater().inflate(R.layout.ll_map_count_info, null);
ImageView imageView = (ImageView)view.findViewById(R.id.iv_maker_bg);
imageView.setImageResource(icon);
TextView txt_count = (TextView) view.findViewById(R.id.txt_count);
if (count > 0) {
txt_count.setText(String.valueOf(count));
txt_count.setVisibility(View.VISIBLE);
} else {
txt_count.setVisibility(View.GONE);
}
return view;
}