今天工作遇到一个需求要一次加载几千条数据,每一条数据都需要对应添加一个图片遮盖物,直接使用添加marker形式进行添加,大量图片的加载会导致APP卡顿,更有甚者会导致闪屏现象,后来查看了下百度地图提供点聚合功能,亲测加载3000个遮盖物不卡顿,集成方案如下:
先上两张demo图
放大后
废话不多说直接上代码
1、首先下载百度地图Demo将clusterutil包复制到自己的项目中,注意一定要保证该util包名和百度原来的包名一致,否则会报错
2、将MarkerClusterDemo类复制到项目
public class MarkerClusterDemo extends Activity implements OnMapLoadedCallback {
MapView mMapView;
BaiduMap mBaiduMap;
MapStatus ms;
private ClusterManager<MyItem> mClusterManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_marker_cluster_demo);
mMapView = (MapView) findViewById(R.id.bmapView);
ms = new MapStatus.Builder().target(new LatLng(39.914935, 116.403119)).zoom(8).build();
mBaiduMap = mMapView.getMap();
mBaiduMap.setOnMapLoadedCallback(this);
mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(ms));
// 定义点聚合管理类ClusterManager
mClusterManager = new ClusterManager<>(this, mBaiduMap);
// 添加Marker点
addMarkers();
// 设置地图监听,当地图状态发生改变时,进行点聚合运算
mBaiduMap.setOnMapStatusChangeListener(mClusterManager);
// 设置maker点击时的响应
mBaiduMap.setOnMarkerClickListener(mClusterManager);
mClusterManager.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<MyItem>() {
@Override
public boolean onClusterClick(Cluster<MyItem> cluster) {
Toast.makeText(MarkerClusterDemo.this,
"有" + cluster.getSize() + "个点", Toast.LENGTH_SHORT).show();
return false;
}
});
mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyItem>() {
@Override
public boolean onClusterItemClick(MyItem item) {
Toast.makeText(MarkerClusterDemo.this,
"点击单个Item", Toast.LENGTH_SHORT).show();
return false;
}
});
}
@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
mMapView.onResume();
super.onResume();
}
@Override
protected void onDestroy() {
mMapView.onDestroy();
super.onDestroy();
}
/**
* 向地图添加Marker点
*/
public void addMarkers() {
// 添加Marker点
LatLng llA = new LatLng(39.963175, 116.400244);
LatLng llB = new LatLng(39.942821, 116.369199);
LatLng llC = new LatLng(39.939723, 116.425541);
LatLng llD = new LatLng(39.906965, 116.401394);
LatLng llE = new LatLng(39.956965, 116.331394);
LatLng llF = new LatLng(39.886965, 116.441394);
LatLng llG = new LatLng(39.996965, 116.411394);
List<MyItem> items = new ArrayList<>();
items.add(new MyItem(llA));
items.add(new MyItem(llB));
items.add(new MyItem(llC));
items.add(new MyItem(llD));
items.add(new MyItem(llE));
items.add(new MyItem(llF));
items.add(new MyItem(llG));
//添加遮盖物数据
for (double i=0;i<3000;i++){
items.add(new MyItem(new LatLng((39.963175+i/10),(116.400244+i/10))));
}
mClusterManager.addItems(items);
}
/**
* 每个Marker点,包含Marker点坐标以及图标
*/
public class MyItem implements ClusterItem {
private final LatLng mPosition;
public MyItem(LatLng latLng) {
mPosition = latLng;
}
@Override
public LatLng getPosition() {
return mPosition;
}
@Override
public BitmapDescriptor getBitmapDescriptor() {
return BitmapDescriptorFactory
.fromResource(R.mipmap.goodcar);
}
}
@Override
public void onMapLoaded() {
// TODO Auto-generated method stub
ms = new MapStatus.Builder().zoom(9).build();
mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(ms));
}
}
3、shift+F10运行,直接解决,再也用不用担心我的APP卡顿了