安卓地图兴趣点 聚合加载
2017-08-28 14:51
357 查看
加载地图 我们一般根据业务需求会添加一些 兴趣点,有时我们需要加载很多,如果按照普通的加载的话,会显得界面很卡顿,所以从网上参考一下资料,这里是聚合形式的方式来实现
代码:如下
主要代码,有问题的朋友可以交流~
代码:如下
/** * 获取视野内的marker 根据聚合算法合成自定义的marker 显示视野内的marker */ private void resetMarks() { System.out.println("markerOptionsList.size():" + markerOptionsList.size()); Projection projection = aMap.getProjection(); Point p = null; markerOptionsListInView.clear(); // 获取在当前视野内的marker;提高效率 for (MarkerOptions mp : markerOptionsList) { p = projection.toScreenLocation(mp.getPosition()); if (p.x < 0 || p.y < 0 || p.x > width || p.y > height) { // 不添加到计算的列表中 } else { markerOptionsListInView.add(mp); } } // 自定义的聚合类MyMarkerCluster ArrayList<MyMarkerCluster> clustersMarker = new ArrayList<MyMarkerCluster>(); for (MarkerOptions mp : markerOptionsListInView) { if (clustersMarker.size() == 0) { clustersMarker.add(new MyMarkerCluster(MainActivity.this, mp, projection, 60));// 100根据自己需求调整 } else { boolean isIn = false; for (MyMarkerCluster cluster : clustersMarker) { if (cluster.getBounds().contains(mp.getPosition())) { cluster.addMarker(mp); isIn = true; break; } } if (!isIn) { clustersMarker.add(new MyMarkerCluster( MainActivity.this, mp, projection, 60)); } } } // 设置聚合点的位置和icon for (MyMarkerCluster mmc : clustersMarker) { mmc.setpositionAndIcon(); } aMap.clear(); // 重新添加 for (MyMarkerCluster cluster : clustersMarker) { aMap.addMarker(cluster.getOptions()); } }
/** * 高德地图聚合 * */ public class MyMarkerCluster { private Activity activity; private MarkerOptions options; private ArrayList<MarkerOptions> includeMarkers; private LatLngBounds bounds;// 创建区域 /** * * @param activity * @param firstMarkers * @param projection * @param gridSize 区域大小参数 */ public MyMarkerCluster(Activity activity, MarkerOptions firstMarkers, Projection projection, int gridSize) { // TODO Auto-generated constructor stub // this.options = firstMarkers; options = new MarkerOptions(); this.activity = activity; Point point = projection.toScreenLocation(firstMarkers.getPosition()); Point southwestPoint = new Point(point.x - gridSize, point.y + gridSize); Point northeastPoint = new Point(point.x + gridSize, point.y - gridSize); bounds = new LatLngBounds( projection.fromScreenLocation(southwestPoint), projection.fromScreenLocation(northeastPoint)); options.anchor(0.5f, 0.5f).title(firstMarkers.getTitle()) .position(firstMarkers.getPosition()) .icon(firstMarkers.getIcon()) .snippet(firstMarkers.getSnippet()); includeMarkers = new ArrayList<MarkerOptions>(); includeMarkers.add(firstMarkers); } /** * 添加marker */ public void addMarker(MarkerOptions markerOptions) { includeMarkers.add(markerOptions);// 添加到列表中 } /** * 设置聚合点的中心位置以及图标 */ public void setpositionAndIcon() { int size = includeMarkers.size(); if (size == 1) { return; } double lat = 0.0; double lng = 0.0; String snippet = ""; for (MarkerOptions op : includeMarkers) { lat += op.getPosition().latitude; lng += op.getPosition().longitude; snippet += op.getTitle() + "\n"; } options.position(new LatLng(lat / size, lng / size));// 设置中心位置为聚集点的平均位置 options.title("聚合点"); options.snippet(snippet); int iconType = size / 10; switch (iconType) { case 0: int identifier = activity.getResources().getIdentifier("marker_cluster_10", "drawable", "com.ez360.cd2016"); options.icon(BitmapDescriptorFactory .fromBitmap(getViewBitmap(getView(size, identifier)))); break; case 1: int identifier_2 = activity.getResources().getIdentifier("marker_cluster_20", "drawable", "com.ez360.cd2016"); options.icon(BitmapDescriptorFactory .fromBitmap(getViewBitmap(getView(size, identifier_2)))); break; case 2: int identifier_3 = activity.getResources().getIdentifier("marker_cluster_30", "drawable", "com.ez360.cd2016"); options.icon(BitmapDescriptorFactory .fromBitmap(getViewBitmap(getView(size, identifier_3)))); break; case 3: int identifier_30 = activity.getResources().getIdentifier("marker_cluster_30", "drawable", "com.ez360.cd2016"); options.icon(BitmapDescriptorFactory .fromBitmap(getViewBitmap(getView(size, identifier_30)))); break; case 4: int identifier_50 = activity.getResources().getIdentifier("marker_cluster_50", "drawable", "com.ez360.cd2016"); options.icon(BitmapDescriptorFactory .fromBitmap(getViewBitmap(getView(size, identifier_50)))); break; default: int identifier_100 = activity.getResources().getIdentifier("marker_cluster_100", "drawable", "com.ez360.cd2016"); options.icon(BitmapDescriptorFactory .fromBitmap(getViewBitmap(getView(size, identifier_100)))); break; } } public LatLngBounds getBounds() { return bounds; } public MarkerOptions getOptions() { return options; } public void setOptions(MarkerOptions options) { this.options = options; } public View getView(int carNum, int resourceId) { View view = activity.getLayoutInflater().inflate( R.layout.my_car_cluster_view, null); TextView carNumTextView = (TextView) view.findViewById(R.id.my_car_num); RelativeLayout myCarLayout = (RelativeLayout) view .findViewById(R.id.my_car_bg); myCarLayout.setBackgroundResource(resourceId); carNumTextView.setText(String.valueOf(carNum)); return view; } /** * 把一个view转化成bitmap对象 */ public static Bitmap getViewBitmap(View view) { view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); view.buildDrawingCache(); Bitmap bitmap = view.getDrawingCache(); return bitmap; } }
主要代码,有问题的朋友可以交流~
相关文章推荐
- MUI 安卓app 地图显示正在加载中
- 安卓开发之地图加载
- 安卓 使用LruCache 加载图片 遇到的问题
- OpenLayers-加载地图数据(Google Map)
- 如何加载全国8000G离线谷歌卫星地图并进行标注
- cocos2dx那些坑(1):瓦片地图加载失败及黑线问题
- Google Maps JavaScript API V3 根据地址 加载地图
- KeyMob-移动广告聚合平台-安卓、苹果应用推广
- 安卓智能地图开发与实施十一:业务数据查询 - ArcGIS Runtime SDK for Android(Version 100.0.0)
- 《ArcGIS Runtime for Android 100.2.1学习笔记》一:配置在线SDK及加载在线地图
- 使用OpenLayers加载高德离线地图简单实用
- 装甲逆袭-地图加载
- ArcGis For Silverlight API,地图显示Gis,绘制点,线,绘制图等(二)--Silverlight 配置动态的 webService、动态加载ArcGis地图服务
- 利用JPCT-AE在安卓设备上进行*.obj文件的加载和显示
- 在Android项目中Fragment加载地图的问题
- 详解安卓从图表icon点击到APP启动界面加载流程
- 使用ArcGIS API for JavaScript 4.3 加载一个瓦片地图
- Openlayers之加载MapQuest地图
- ionic3 懒加载 和安卓双击退出应用 动态加载图片
- 游戏加载地图后 SetProcessWorkingSetSize 用例