上一篇说了在地图上实现了自定义Markers,但是markers太多在地图上显示的就会密密麻麻,重叠覆盖,这里就介绍一下markers的聚合。先看一下封装好的聚合类。
public class MarkerClusterYellow { private Activity activity; private MarkerOptions options; private ArrayList<MarkerOptions>includeMarkers; private LatLngBounds bounds;// 创建区域 /** * * @param activity * @param firstMarkers * @param projection * @param gridSize * 区域大小参数 */ public MarkerClusterYellow(Activity activity, MarkerOptions firstMarkers, Projection projection, int gridSize) { 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(String text) { String id=""; 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"; id=id+op.getTitle()+","; } options.position(new LatLng(lat / size, lng / size));// 设置中心位置为聚集点的平均距离 options.title(id); options.snippet(snippet); int iconType = size / 2; switch (iconType) { default: options.icon(BitmapDescriptorFactory .fromBitmap(getViewBitmap(getView(size,text, R.mipmap.content_icon_positions_yellow)))); break; } } public LatLngBounds getBounds() { return bounds; } public MarkerOptions getOptions() { return options; } public void setOptions(MarkerOptions options) { this.options = options; } public View getView(int carNum,String text,int resourceId) { View view = activity.getLayoutInflater().inflate(R.layout.my_car_cluster_view, null); TextView carNumTextView = (TextView) view.findViewById(R.id.my_car_num); TextView tv_price = (TextView) view.findViewById(R.id.tv_price); TextView tv_price_status = (TextView) view.findViewById(R.id.tv_price_status); tv_price.setText(text); tv_price_status.setText("元/天"); tv_price.setTextColor(Color.parseColor("#FFBB18")); tv_price_status.setTextColor(Color.parseColor("#FFBB18")); LinearLayout myCarLayout = (LinearLayout) 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; } }
在前一篇博客的基础上我们进行聚合,只需要做以下操作:
public class FragmentMap extends Fragment implements LocationSource, AMapLocationListener,OnCameraChangeListener { private static FragmentMap fragment = null; @ViewInject(R.id.map) private MapView mapView; private AMap aMap; private View mapLayout; private OnLocationChangedListener mListener; private LocationManagerProxy mAMapLocationManager; private List<PositionEneity> positionEneityList = new ArrayList<PositionEneity>(); private ArrayList<MarkerOptions> markerOptionsListYellow = new ArrayList<MarkerOptions>();// 所有的marker private ArrayList<MarkerOptions> markerOptionsListInViewYellow= new ArrayList<MarkerOptions>();// 视野内的marker String yellow=""; private int height;// 屏幕高度(px) private int width;// 屏幕宽度(px) private int gridSize = 50;// marker点区域大小 Handler handler = new Handler() { @Override public void handleMessage(android.os.Message msg) { super.handleMessage(msg); if (msg.what == 0) { resetMarks();// 更新markers } } }; public static Fragment newInstance() { if (fragment == null) { synchronized (FragmentMap.class) { if (fragment == null) { fragment = new FragmentMap(); } } } return fragment; } public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { if (mapLayout == null) { mapLayout = inflater.inflate(R.layout.fragment_map, null); ViewUtils.inject(this, mapLayout); mapView.onCreate(savedInstanceState); DisplayMetrics dm = new DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm); width = dm.widthPixels; height = dm.heightPixels; if (aMap == null) { aMap = mapView.getMap(); aMap.setLocationSource(this);// 设置定位监听 aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示 aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false } } else { if (mapLayout.getParent() != null) { ((ViewGroup) mapLayout.getParent()).removeView(mapLayout); } } aMap.setOnCameraChangeListener(this); return mapLayout; } /** * 获取视野内的marker 根据聚合算法合成自定义的marker 显示视野内的marker */ <strong> private void resetMarks() { // 开始刷新界面 Projection projection = aMap.getProjection(); Point p = null; markerOptionsListInViewYellow.clear(); // 获取在当前视野内的marker;提高效率 for (MarkerOptions mp : markerOptionsListYellow) { p = projection.toScreenLocation(mp.getPosition()); if (p.x < 0 || p.y < 0 || p.x > width || p.y > height) { // 不添加到计算的列表中 } else { markerOptionsListInViewYellow.add(mp); } } // 自定义的聚合类MarkerCluster ArrayList<MarkerClusterYellow> clustersMarkeryellow = new ArrayList<MarkerClusterYellow>(); for (MarkerOptions mp : markerOptionsListInViewYellow) { if (clustersMarkeryellow.size() == 0) { clustersMarkeryellow.add(new MarkerClusterYellow(getActivity(), mp, projection, gridSize));//gridSize 根据自己需求调整 } else { boolean isIn = false; for (MarkerClusterYellow cluster : clustersMarkeryellow) { if (cluster.getBounds().contains(mp.getPosition())) { cluster.addMarker(mp); isIn = true; break; } } if (!isIn) { clustersMarkeryellow.add(new MarkerClusterYellow(getActivity(), mp, projection, gridSize)); } } } // 先清除地图上所有覆盖物 aMap.clear(); for (MarkerClusterYellow markerClusterYellow : clustersMarkeryellow) { markerClusterYellow.setpositionAndIcon(yellow);// 设置聚合点的位置和icon aMap.addMarker(markerClusterYellow.getOptions());// 重新添加 } } private void initview() { for (int i = 0; i < positionEneityList.size(); i++) { if (positionEneityList.get(i).getType().equals("2")) { yellow=positionEneityList.get(i).getPrice(); View view01 = View.inflate(getActivity(),R.layout.view_everyday, null); TextView tv_price = (TextView) view01.findViewById(R.id.tv_price); TextView tv_price_status = (TextView) view01.findViewById(R.id.tv_price_status); tv_price.setText(positionEneityList.get(i).getPrice()); tv_price_status.setText("元/天"); Bitmap bitmap = CommentActivity.convertViewToBitmap(view01); <strong> markerOptionsListYellow.add(new MarkerOptions() .position(new LatLng(Double.parseDouble(positionEneityList.get(i).getLatitude()) , Double.parseDouble(positionEneityList.get(i).getLongitude()))).icon(BitmapDescriptorFactory.fromBitmap(bitmap)) .title(positionEneityList.get(i).getId())); } } } @Override public void onCameraChange(CameraPosition cameraPosition) { } @Override public void onCameraChangeFinish(CameraPosition cameraPosition) { handler.sendEmptyMessage(0);// 更新界面marker } }
定位和地图显示部分请参考之前的博客:
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索int
, arraylist
, new
null
android 高德地图聚合、android 高德聚合点、android 高德地图开发、android 高德导航开发、高德地图点聚合,以便于您获取更多的相关知识。
时间: 2024-09-14 09:41:53