在百度地图的基础地图上循环添加多个Mark点,并且对每个Mark点进行点击事件。
业务场景描述:
1.请求服务器得到多个地址(最好比较详细的地址)
2.拿到地址进行地理编码得到经纬度
3.循环添加Mark点
4.对Mark点进行点击事件
废话少说先来几张效果图:
开发步骤:
1.加载基础地图 (不多说)
2.新建地图要展示的实体类Bean对象 如下:
public class mapInfoBean implements Serializable{ public String debtor; public String visitState; public String customer; public String addressDeail; public String caseCodeNumber; public String batchNumber; public String getDebtor() { return debtor; } public void setDebtor(String debtor) { this.debtor = debtor; } public String getVisitState() { return visitState; } public void setVisitState(String visitState) { this.visitState = visitState; } public String getCustomer() { return customer; } public void setCustomer(String customer) { this.customer = customer; } public String getAddressDeail() { return addressDeail; } public void setAddressDeail(String addressDeail) { this.addressDeail = addressDeail; } public String getCaseCodeNumber() { return caseCodeNumber; } public void setCaseCodeNumber(String caseCodeNumber) { this.caseCodeNumber = caseCodeNumber; } public String getBatchNumber() { return batchNumber; } public void setBatchNumber(String batchNumber) { this.batchNumber = batchNumber; } public mapInfoBean(String debtor, String visitState, String customer, String addressDeail, String caseCodeNumber, String batchNumber) { this.debtor = debtor; this.visitState = visitState; this.customer = customer; this.addressDeail = addressDeail; this.caseCodeNumber = caseCodeNumber; this.batchNumber = batchNumber; } }
3. 遍历服务器地址 ,并进行编码 这里是循环添加多个展示对象 和遍历多个地址 ,主题代码为:
if (!TextUtils.isEmpty(address)){ if (!addressData.contains(address)){ //过滤掉相同的地址信息 addressData.add(address); mSearch.geocode(new GeoCodeOption() //进行编码 .city("") .address(address)); logUtils.d("地址反编码"+":"+address); mapInfoData.add(new mapInfoBean(name,next.getVisitStatusText(),next.getCustomerName(),next.getAddress(),next.getCaseCode(),next.getVisitGuid())); } }
4.在编码的回调方法中循环添加Mark
OnGetGeoCoderResultListener GeoListener = new OnGetGeoCoderResultListener() { @Override public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) { if (null != geoCodeResult && null != geoCodeResult.getLocation()) { if (geoCodeResult == null || geoCodeResult.error != SearchResult.ERRORNO.NO_ERROR) { //没有检索到结果 return; } else { service_flag=true; double latitude = geoCodeResult.getLocation().latitude; double longitude = geoCodeResult.getLocation().longitude; LatLng latLng=new LatLng(latitude,longitude ); logUtils.d("地理反编码地址"+"latitude"+latitude+"longitude"+longitude); servicePoint = new LatLng(latitude, longitude); //创建OverlayOptions属性 // lv_mainItemPostion mapInfoBean mapInfoBean=null; if (!click_address_item_flag){ mapInfoBean = mapInfoData.get(currrentCount); currrentCount++; //循环 }else { mapInfoBean = mapInfoData.get(lv_mainItemPostion); } BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.mipmap.man); OverlayOptions option1 = new MarkerOptions() .position(servicePoint) .icon(bitmap); marker = (Marker) map.addOverlay(option1); Bundle bundle=new Bundle(); bundle.putSerializable("info", mapInfoBean); //携带对象数据 marker.setExtraInfo(bundle); MapStatus mMapStatus = new MapStatus.Builder() .target(latLng) .zoom(15) .build(); //定义MapStatusUpdate对象,以便描述地图状态将要发生的变化 MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus); //改变地图状态 map.animateMapStatus(mMapStatusUpdate); } } } @Override public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) { } };
5. 对mark 的点击事件
private void onClickMark(Marker marker) { Bundle bundle = marker.getExtraInfo(); if (bundle !=null) { final mapInfoBean bean = (mapInfoBean) bundle.getSerializable("info"); //拿到实体类 就好办事
}
总结:
就是用Bundle传递数据,得到对应的实体