一、牛人们的想法
下面是我参考的有关的博客,下面将一一列举
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.下面贴出代码
- <spanstyle="font-family:KaiTi_GB2312;">spanstyle="font-size:18px;">//cluster聚合器类
- packagecom.zhl.map;
- importjava.util.ArrayList;
- importjava.util.List;
- importandroid.graphics.Bitmap;
- importandroid.graphics.Point;
- importandroid.graphics.drawable.BitmapDrawable;
- importandroid.util.Log;
- importandroid.view.LayoutInflater;
- importandroid.view.View;
- importandroid.widget.TextView;
- importbaidumapsdk.demo.R;
- importcom.baidu.mapapi.cloud.Bounds;
- importcom.baidu.mapapi.map.MapView;
- importcom.baidu.mapapi.map.OverlayItem;
- importcom.baidu.mapapi.utils.DistanceUtil;
- importcom.baidu.platform.comapi.basestruct.GeoPoint;
- importcom.zhl.activity.MarkerClusterActivity;
- importcom.zhl.util.MapUtils;
- publicclassCluster{
- privateMarkerClusterActivitymMarkCluster;
- privateMapViewmMapView;
- privateintmMinClusterSize;
- privateBooleanisAverageCenter;
- privateintmGridSize;
- privatedoublemDistance;
- privateListClusterMarker>mMarkers;
- publicCluster(MarkerClusterActivitymarkCluster,MapViewmapView
- ,intminClusterSize,BooleanisAverageCenter
- ,intmGridSize,doublemDistance){
- this.mMarkCluster=markCluster;
- this.mMapView=mapView;
- this.mMinClusterSize=minClusterSize;
- this.isAverageCenter=isAverageCenter;
- this.mGridSize=mGridSize;
- this.mDistance=mDistance;
- mMarkers=newArrayList>();
- }
- publicListOverlayItem>createCluster(List>markerList){
- this.mMarkers.clear();
- List>itemList=newArrayList>();
- for(inti=0;imarkerList.size();i++){
- addCluster(markerList.get(i));
- }
- for(inti=0;imMarkers.size();i++){
- ClusterMarkercm=mMarkers.get(i);
- setClusterDrawable(cm);
- OverlayItemoi=newOverlayItem(cm.getmCenter(),cm.getTitle(),cm.getSnippet());
- oi.setMarker(cm.getMarker());
- itemList.add(oi);
- returnitemList;
- privatevoidaddCluster(OverlayItemmarker){
- GeoPointmarkGeo=marker.getPoint();
- if(mMarkers.size()==0){
- ClusterMarkerclusterMarker=newClusterMarker(marker.getPoint(),marker.getTitle(),marker.getSnippet());
- clusterMarker.setMarker(marker.getMarker());
- clusterMarker.AddMarker(marker,isAverageCenter);
- Boundsbound=newBounds(markGeo.getLatitudeE6(),markGeo.getLongitudeE6(),markGeo.getLatitudeE6(),markGeo.getLongitudeE6());
- bound=MapUtils.getExtendedBounds(mMapView,bound,mGridSize);
- clusterMarker.setmGridBounds(bound);
- mMarkers.add(clusterMarker);
- }else{
- ClusterMarkerclusterContain=null;
- doubledistance=mDistance;
- mMarkers.size();i++){
- ClusterMarkerclusterMarker=mMarkers.get(i);
- GeoPointcenter=clusterMarker.getmCenter();
- doubled=DistanceUtil.getDistance(center,marker.getPoint());
- if(ddistance){
- distance=d;
- clusterContain=clusterMarker;
- if(clusterContain==null||!isMarkersInCluster(markGeo,clusterContain.getmGridBounds())){
- clusterMarker.AddMarker(marker,isAverageCenter);
- Boundsbound=newBounds(markGeo.getLatitudeE6(),markGeo.getLongitudeE6());bound=MapUtils.getExtendedBounds(mMapView,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> mMarkers.add(clusterMarker);
- }else{
- clusterContain.AddMarker(marker,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> privatevoidsetClusterDrawable(ClusterMarkerclusterMarker){
- ViewdrawableView=LayoutInflater.from(mMarkCluster).inflate(
- R.layout.drawable_mark,null);
- TextViewtext=(TextView)drawableView.findViewById(R.id.drawble_mark);
- intmarkNum=clusterMarker.getmMarkers().size();
- if(markNum>=2){
- text.setText(markNum+"");
- 11){
- text.setBackgroundResource(R.drawable.m0);
- }elseif(markNum>10&&markNum21){
- text.setBackgroundResource(R.drawable.m1);
- >20&&markNum31){
- text.setBackgroundResource(R.drawable.m2);
- >30&&markNum41){
- text.setBackgroundResource(R.drawable.m3);
- text.setBackgroundResource(R.drawable.m4);
- Bitmapbitmap=MapUtils.convertViewToBitmap(drawableView);
- clusterMarker.setMarker(newBitmapDrawable(bitmap));
- privateBooleanisMarkersInCluster(GeoPointmarkerGeo,Boundsbound){
- if(markerGeo.getLatitudeE6()>bound.leftBottom.getLatitudeE6()
- &&markerGeo.getLatitudeE6()bound.rightTop.getLatitudeE6()
- &&markerGeo.getLongitudeE6()>bound.rightTop.getLongitudeE6()
- &&markerGeo.getLongitudeE6()bound.leftBottom.getLongitudeE6()){
- returntrue;
- returnfalse;
- }</span>
3.工程demo代码下载,地图标注物聚合.zip
4.下面是程序的实例图片