Marker多点聚合计算
什么是多点聚合呢?所谓的多点聚合,就是计算地图上一定范围内的点的个数,然和讲所有的点统计的数量显示在范围内设置的marker上;我们需要解决的问题有:1、在地图上划定区域范围;2、计算范围内的点的个数;3、清楚原有的点,加上聚合后的点;4、如何刷新
先说第一个问题:
在地图上划定区域范围,如下代码实现:
Point mPoint 定义一个点,这个点是通过地图上的marker点在屏幕上的投影的点
划定区域范围
如上图,我们在划定区域的时候以西南角和东北角(手机屏幕上表现为右下角和左上角)的两个点直线距离为直径划定圆形区域,圆心为mPiont点所在的位置
southwestPoint的X轴上的值要小于northeastPoint的X轴上的值
northeastPoint的Y轴上的值要小于northeastPoint的Y轴上的值
southwestPoint 西南角
northeastPoint 东北角
Point mPoint = mProjection.toScreenLocation(mFirstMarkers.getPosition());
Point southwestPoint = new Point(mPoint.x - mGridSize,mPoint.y + mGridSize);
Point northeastPoint = new Point(mPoint.x + mGridSize,mPoint.y - mGridSize);
double northELat = mProjection.fromScreenLocation(northeastPoint).latitude;
double southWLat = mProjection.fromScreenLocation(southwestPoint).latitude;
double northElong = mProjection.fromScreenLocation(northeastPoint).longitude;
double southWlong = mProjection.fromScreenLocation(southwestPoint).longitude;
if(northELat > southWLat && northElong > southWlong) {
LatLng southWestLat = new LatLng(southWLat, southWlong);
LatLng northEastLat = new LatLng(northELat, northElong);
mBounds = new LatLngBounds(southWestLat, northEastLat);
}
添加聚合后的marker点
mMarkerOptions.anchor(0.5f,0.5f)
.position(mFirstMarkers.getPosition())
.icon(mFirstMarkers.getIcon());
mIncludeMarkers = new ArrayList<MarkerOptions>();
mIncludeMarkers.add(mFirstMarkers);
获取聚合后Marker点上的数量
int num = mIncludeMarkers.size();
设置marker的style样式和数据
int iconType = 0;
if(num < 100){
iconType = num / 10;
}else if(num >= 100){
iconType = (num / 100)/10;
}
switch (iconType) {
case 0:
mMarkerOptions.icon(BitmapDescriptorFactory
.fromBitmap(getViewBitmap(getView(num,
R.mipmap.map_marker_num))));
break;
case 1:
mMarkerOptions.icon(BitmapDescriptorFactory
.fromBitmap(getViewBitmap(getView(num,
R.mipmap.map_marker_num))));
break;
case 2:
mMarkerOptions.icon(BitmapDescriptorFactory
.fromBitmap(getViewBitmap(getView(num,
R.mipmap.map_marker_num))));
break;
case 3:
mMarkerOptions.icon(BitmapDescriptorFactory
.fromBitmap(getViewBitmap(getView(num,
R.mipmap.map_marker_num))));
break;
case 4:
mMarkerOptions.icon(BitmapDescriptorFactory
.fromBitmap(getViewBitmap(getView(num,
R.mipmap.map_marker_num))));
break;
case 5:
mMarkerOptions.icon(BitmapDescriptorFactory
.fromBitmap(getViewBitmap(getView(num,
R.mipmap.map_marker_num))));
break;
case 6:
mMarkerOptions.icon(BitmapDescriptorFactory
.fromBitmap(getViewBitmap(getView(num,
R.mipmap.map_marker_num))));
break;
case 7:
mMarkerOptions.icon(BitmapDescriptorFactory
.fromBitmap(getViewBitmap(getView(num,
R.mipmap.map_marker_num))));
break;
case 8:
mMarkerOptions.icon(BitmapDescriptorFactory
.fromBitmap(getViewBitmap(getView(num,
R.mipmap.map_marker_num))));
break;
case 9:
mMarkerOptions.icon(BitmapDescriptorFactory
.fromBitmap(getViewBitmap(getView(num,
R.mipmap.map_marker_num))));
break;
}
根据地图的移动重置和刷新每次的聚合后的点和数字,因为高德地图本身提供的地图移动的监听的方法,地图的放大缩小,移动都归属于地图的移动,我们只需要在地图移动的时候,我们需要通过Handler将Message传到子线程,当msg.what为0 的时候来重置地图上的所有Marker,
Handler timeHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 0:
// 更新markers
resetMarkers();
break;
}
}
};
补充(重置marker,参见代码):
/**
* 获取视野内的marker 根据聚合算法合成自定义的marker 显示视野内的marker
*/
private void resetMarkers() {
/*
* 返回一个Projection 对象。可以通过这个对象在屏幕坐标与经纬度坐标之间进行转换。
* Projection 对象返回的是当前可视区域的坐标,当可视区域变换时,它不会自己更新。
* 返回:
* 当前地图位置的Projection对象。
*/
Projection projection = aMap.getProjection();
Point point = null;
markerOptionsListInView.clear();
for (MarkerOptions markerOptions : markerOptionsList) {
point = projection.toScreenLocation(markerOptions.getPosition());
if (point.x < 0 || point.y < 0 || point.x > width || point.y > height) {
} else {
markerOptionsListInView.add(markerOptions);
}
}
// 自定义的聚合类mSetMarkerIcon
ArrayList<SetMarkerIcon> mSetMarkerIcon = new ArrayList<SetMarkerIcon>();
for (MarkerOptions mp : markerOptionsListInView) {
if (mSetMarkerIcon.size() == 0) {
mSetMarkerIcon.add(new SetMarkerIcon(MapActivity.this,
mp, projection, 80));// 100根据自己需求调整
} else {
boolean isIn = false;
for (SetMarkerIcon cluster : mSetMarkerIcon) {
if (cluster.getBounds() != null) {
if (cluster.getBounds().contains(mp.getPosition())) {
cluster.addMarker(mp);
isIn = true;
break;
}
}
}
if (!isIn) {
mSetMarkerIcon.add(new SetMarkerIcon(
MapActivity.this, mp, projection, 80));
}
}
}
// 设置聚合点的位置和icon
for (SetMarkerIcon setMarkerIcon : mSetMarkerIcon) {
setMarkerIcon.setPositionAndIcon();
}
//清楚marker
aMap.clear(true);
// 重新添加
for (SetMarkerIcon markerIcon : mSetMarkerIcon) {
aMap.addMarker(markerIcon.getOptions());
}
}
Marker点击
首先需要在初始化的时候添加Marker点击的监听
aMap.setOnMarkerClickListener(this);// 设置点击marker事件监听器
点击事件的处理
/**
* 对marker标注点点击响应事件
*/
@Override
public boolean onMarkerClick(Marker marker) {
//此处添加Marker点击事件的处理
return false;
}
以前写的文章没有发表的,仅供参考和学习只用
谢谢惠顾,感觉有用的点个赞