安卓地图兴趣点 聚合加载
来源:互联网 发布:淘宝查看vip等级 编辑:程序博客网 时间:2024/05/22 14:18
加载地图 我们一般根据业务需求会添加一些 兴趣点,有时我们需要加载很多,如果按照普通的加载的话,会显得界面很卡顿,所以从网上参考一下资料,这里是聚合形式的方式来实现
代码:如下
/** * 获取视野内的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; }}
主要代码,有问题的朋友可以交流~
阅读全文
0 0
- 安卓地图兴趣点 聚合加载
- 百度地图加载海量点、聚合
- 百度地图兴趣点检索
- 百度地图聚合点
- android地图点聚合
- 百度地图之周边兴趣点搜索
- 百度地图兴趣点抓取工具
- 地图API自定义点聚合
- 百度地图点聚合提高效率
- 安卓高德地图点聚合功能
- 百度地图点聚合改造
- 安卓开发之地图加载
- 百度地图点聚合的聚合点样式修改
- Android百度地图点聚合功能
- Android:百度地图 点聚合功能
- 百度地图聚合点的展示
- 百度地图点聚合-设置lable显示
- 使用百度地图的点聚合功能
- 基于TCP协议的Java聊天小程序
- linux下mysql表名大小写敏感问题
- Eclipse Maven 编译错误 Dynamic Web Module 3.0 requires Java 1.6 or newer 解决方案
- 存储过程 查询
- git学习2
- 安卓地图兴趣点 聚合加载
- golang 文件读取
- android缩放图片添加文字信息
- Android端 APP GPU过度绘制及优化
- HMM MEMM CRF 区别 联系
- opencv学习
- Lucene打分公式的数学推导
- 关于 Error:Failed to resolve: androidAppFreamwork:baselibrary:unspecified 的解决方式
- MinGW MinGW-w64 TDM-GCC等工具链之间的区别与联系