俗话说站在巨人的肩膀上将事半功倍,在写android的百度地图标注物聚合时,我在网上也进行了大量的查询,发现标注物聚合的算法很早就有人写了,不过他们是js或者是Google地图c#版的。借鉴他们的想法,我写了android的这版代码与大家一起分享。
一、牛人们的想法
下面是我参考的有关的博客,下面将一一列举
1.MarkerCluster之百度地图版 http://hi.baidu.com/liongg/item/d8adece188fbfb11585dd89f
2.Marker Cluster面面观http://hi.baidu.com/liongg/item/a380cc95bd70c2bdcd80e581
3.GoogleMap标注物聚合解决办法http://www.svennerberg.com/2009/01/handling-large-amounts-of-markers-in-google-maps/
百度地图javascript开源库:http://developer.baidu.com/map/library.htm
二、我的思路
1.下面以一个流程图来表述我的想法。
2.下面贴出代码
//cluster聚合器类 package com.zhl.map; import java.util.ArrayList; import java.util.List; import android.graphics.Bitmap; import android.graphics.Point; import android.graphics.drawable.BitmapDrawable; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.TextView; import baidumapsdk.demo.R; import com.baidu.mapapi.cloud.Bounds; import com.baidu.mapapi.map.MapView; import com.baidu.mapapi.map.OverlayItem; import com.baidu.mapapi.utils.DistanceUtil; import com.baidu.platform.comapi.basestruct.GeoPoint; import com.zhl.activity.MarkerClusterActivity; import com.zhl.util.MapUtils; public class Cluster{ private MarkerClusterActivity mMarkCluster; private MapView mMapView; private int mMinClusterSize; private Boolean isAverageCenter; private int mGridSize; private double mDistance; private List<ClusterMarker> mMarkers; public Cluster(MarkerClusterActivity markCluster,MapView mapView,int minClusterSize,Boolean isAverageCenter,int mGridSize,double mDistance) { this.mMarkCluster = markCluster; this.mMapView = mapView; this.mMinClusterSize = minClusterSize; this.isAverageCenter = isAverageCenter; this.mGridSize = mGridSize; this.mDistance = mDistance; mMarkers = new ArrayList<ClusterMarker>(); } public List<OverlayItem> createCluster(List<OverlayItem> markerList){ this.mMarkers.clear(); List<OverlayItem> itemList = new ArrayList<OverlayItem>(); for(int i=0;i<markerList.size();i++){ addCluster(markerList.get(i)); } for(int i=0;i<mMarkers.size();i++){ ClusterMarker cm = mMarkers.get(i); setClusterDrawable(cm); OverlayItem oi = new OverlayItem(cm.getmCenter(),cm.getTitle(),cm.getSnippet()); oi.setMarker(cm.getMarker()); itemList.add(oi); } return itemList; } private void addCluster(OverlayItem marker){ GeoPoint markGeo = marker.getPoint(); if(mMarkers.size()==0){ ClusterMarker clusterMarker = new ClusterMarker(marker.getPoint(),marker.getTitle(),marker.getSnippet()); clusterMarker.setMarker(marker.getMarker()); clusterMarker.AddMarker(marker,isAverageCenter); Bounds bound = new Bounds(markGeo.getLatitudeE6(),markGeo.getLongitudeE6(),markGeo.getLatitudeE6(),markGeo.getLongitudeE6()); bound = MapUtils.getExtendedBounds(mMapView,bound,mGridSize); clusterMarker.setmGridBounds(bound); mMarkers.add(clusterMarker); }else{ ClusterMarker clusterContain = null; double distance = mDistance; for(int i=0;i<mMarkers.size();i++){ ClusterMarker clusterMarker = mMarkers.get(i); GeoPoint center = clusterMarker.getmCenter(); double d = DistanceUtil.getDistance(center,marker.getPoint()); if(d<distance){ distance = d; clusterContain = clusterMarker; } } if(clusterContain == null||!isMarkersInCluster(markGeo,clusterContain.getmGridBounds())){ ClusterMarker clusterMarker = new ClusterMarker(marker.getPoint(),marker.getSnippet()); clusterMarker.AddMarker(marker,isAverageCenter); clusterMarker.AddMarker(marker,mGridSize); clusterMarker.setmGridBounds(bound); mMarkers.add(clusterMarker); }else{ clusterContain.AddMarker(marker,isAverageCenter); } } } private void setClusterDrawable(ClusterMarker clusterMarker){ View drawableView = LayoutInflater.from(mMarkCluster).inflate( R.layout.drawable_mark,null); TextView text = (TextView) drawableView.findViewById(R.id.drawble_mark); int markNum = clusterMarker.getmMarkers().size(); if(markNum>=2){ text.setText(markNum+""); if(markNum<11){ text.setBackgroundResource(R.drawable.m0); }else if(markNum>10&&markNum<21){ text.setBackgroundResource(R.drawable.m1); }else if(markNum>20&&markNum<31){ text.setBackgroundResource(R.drawable.m2); }else if(markNum>30&&markNum<41){ text.setBackgroundResource(R.drawable.m3); }else{ text.setBackgroundResource(R.drawable.m4); } Bitmap bitmap = MapUtils.convertViewToBitmap(drawableView); clusterMarker.setMarker(new BitmapDrawable(bitmap)); }else{ } } private Boolean isMarkersInCluster(GeoPoint markerGeo,Bounds bound){ if(markerGeo.getLatitudeE6()>bound.leftBottom.getLatitudeE6() &&markerGeo.getLatitudeE6()<bound.rightTop.getLatitudeE6() &&markerGeo.getLongitudeE6()>bound.rightTop.getLongitudeE6() &&markerGeo.getLongitudeE6()<bound.leftBottom.getLongitudeE6()){ return true; } return false; } }
3.工程demo代码下载,地图标注物聚合.zip
4.下面是程序的实例图片