Android整合百度地圖點聚合
阿新 • • 發佈:2019-02-08
在專案中集成了百度地圖,剛開始Mark點少的時候滑動很流暢,後來將資料匯入有三千多個Mark點,這時候滑動地圖已經卡死了,只能換成點聚合了,先把需要用到的類從百度的示例專案複製到當前專案中,
需要用到如上標註的類,上程式碼:
效果圖如下:public class BdMapFragment extends Fragment { @BindView(R.id.iv_addhouse) ImageView ivAddhouse; @BindView(R.id.ll_addhouse) LinearLayout llAddhouse; @BindView(R.id.iv_position) ImageView ivPosition; @BindView(R.id.et_search) EditText etSearch; @BindView(R.id.ll_search) LinearLayout llSearch; @BindView(R.id.start) Button start; @BindView(R.id.end) Button end; @BindView(R.id.route) Button route; @BindView(R.id.reset) Button reset; @BindView(R.id.ll_guidelayout) LinearLayout llGuidelayout; private boolean addHouse = false; private LocationClient mLocationClient; public BDLocationListener myListener; private ArrayList<PolyOverlyInfo> polyInfoList = new ArrayList<>(); private View view; public static MapView mMapView = null; //private OverItemT mItemOverlay = null, serarchItemOverlay; private List<HousemarkBean> houseMarkList; private List<HousemarkBean> searchMarkList = new ArrayList<>(); private static final String TAG = "BdMapFragment"; private boolean isFrist = true; private BaiduMap mBaiduMap; private Polyline mPolyline; private Marker marker,housemarker; private BitmapDescriptor bitmapred, bitmapblue; private static final int BAIDU_READ_PHONE_STATE =100; private ClusterManager<MyItem> mClusterManager; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //在使用SDK各元件之前初始化context資訊,傳入ApplicationContext //注意該方法要再setContentView方法之前實現 view = LayoutInflater.from(getActivity()).inflate(R.layout.bdfragment_map, null); ButterKnife.bind(this, view); mMapView = (MapView) view.findViewById(R.id.mapview); bitmapred = BitmapDescriptorFactory.fromResource(R.drawable.position); mMapView.setLogoPosition(LogoPosition.logoPostionleftTop); mMapView.showZoomControls(false); mBaiduMap = mMapView.getMap(); //普通地圖 mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL); // 定義點聚合管理類ClusterManager mClusterManager = new ClusterManager<MyItem>(getActivity(), mBaiduMap); mBaiduMap.setOnMapLoadedCallback(new BaiduMap.OnMapLoadedCallback() {//地圖載入完成後 @Override public void onMapLoaded() { getAddressPoints(); } }); mBaiduMap.setOnMapStatusChangeListener(mClusterManager); // 設定maker點選時的響應 mBaiduMap.setOnMarkerClickListener(mClusterManager); mClusterManager.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<MyItem>() { @Override public boolean onClusterClick(Cluster<MyItem> cluster) { Toast.makeText(getActivity(), "有" + cluster.getSize() + "個點", Toast.LENGTH_SHORT).show(); return false; } }); return view; } /** * 請求危房資訊資料 */ private void getAddressPoints() { houseMarkList = new ArrayList<>(); Map<String, Object> params=new HashMap(); params.put("regionCode", SPUtils.getregionCode()); RetrofitManager.getInstance().createReq(MarkApi.class).getHouseInfo(params) .enqueue(new Callback<BaseBean<List<HousemarkBean>>>() { @Override public void onResponse(Call<BaseBean<List<HousemarkBean>>> call, Response<BaseBean<List<HousemarkBean>>> response) { if (response.body() != null && response.body().getCode() == 1) { houseMarkList = response.body().getRows(); addMark(houseMarkList);//新增Mark } } @Override public void onFailure(Call<BaseBean<List<HousemarkBean>>> call, Throwable t) { t.printStackTrace(); } }); } /** * 新增mark * * @param list 危房資訊集合 */ private void addMark(List<HousemarkBean> list) { List<MyItem> items = new ArrayList<MyItem>(); for (HousemarkBean houseInfo : list) { LatLng latlng = PositionUtil.gpstoBd(new LatLng(houseInfo.getY(), houseInfo.getX())); items.add(new MyItem(latlng,houseInfo)); } mClusterManager.addItems(items); } @Override public void onDestroyView() { // 關閉定點陣圖層 mBaiduMap.setMyLocationEnabled(false); mMapView.onDestroy(); mMapView = null; super.onDestroyView(); } @Override public void onResume() { // MapView的生命週期與Activity同步,當activity恢復時需呼叫MapView.onResume() mMapView.onResume(); super.onResume(); } @Override public void onPause() { mMapView.onPause(); super.onPause(); } /** * @paramgeoPoint當前點選的點 * @return當前點選位置所在街道資訊 */ private PolyOverlyInfo getPolyInfo(LatLng point) { for (PolyOverlyInfo polygonInfo : polyInfoList) { if (PointUtils.IsPointInPolygon(polygonInfo.getPolyline().getPoints(), point)) { return polygonInfo; } } return null; } /** * 每個Marker點,包含Marker點座標以及圖示 */ public class MyItem implements ClusterItem { private LatLng mPosition; private HousemarkBean mhousemarkBean; public MyItem(LatLng latLng,HousemarkBean housemarkBean) { mPosition = latLng; mhousemarkBean=housemarkBean; } @Override public LatLng getPosition() { return mPosition; } @Override public BitmapDescriptor getBitmapDescriptor() { return BitmapDescriptorFactory .fromResource(R.drawable.position); } } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); EventBus.getDefault().register(this); } @Override public void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } }