地图选点
功能场景
网约车APP中支持用户通过滑动地图快速选择起终点。
//mapopen-website-wiki.bj.bcebos.com/demos/AndroidVideos/地图选点.mp4
location(LatLng location)
newVersion(int latestAdmin)
reverseGeoCode(ReverseGeoCodeOption option)
setOnGetGeoCodeResultListener(OnGetGeoCoderResultListener listener)
务必在Activity的onDestroy函数里,调用MapView和GeoCoder的销毁方法,否则会有内存泄露。
@Override
protected void onDestroy(){
super.onDestroy();
if(null!= mGeoCoder){
mGeoCoder.destroy();
}
if(null!= mMapView){
mMapView.onDestroy();
}
}
复制成功
/**
/**
* 逆地理编码请求
* @param latLng
*/
private void reverseRequest(LatLng latLng){
if(null == latLng){
return;
}
ReverseGeoCodeOption reverseGeoCodeOption = new ReverseGeoCodeOption().location(latLng)
.newVersion(1)
.radius(sDefaultRGCRadius)
.pageNum(mLoadIndex);
if(null == mGeoCoder){
mGeoCoder = GeoCoder.newInstance();
}
mGeoCoder.setOnGetGeoCodeResultListener(this);
mGeoCoder.reverseGeoCode(reverseGeoCodeOption);
}
复制成功
/**
@Override
public void onGetReverseGeoCodeResult(final ReverseGeoCodeResult reverseGeoCodeResult) {
if (null == reverseGeoCodeResult) {
return;
}
mHandler.post(new Runnable() {
@Override
public void run() {
updateUI(reverseGeoCodeResult);
}
});
}
复制成功
//mapopen-website-wiki.bj.bcebos.com/demos/iosVideos/iOS地图选点.mov
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation;
- (void)mapView:(BMKMapView *)mapView regionWillChangeAnimated:(BOOL)animated reason:(BMKRegionChangeReason)reason;
- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated reason:(BMKRegionChangeReason)reason;
CLLocationCoordinate2D location
BMKReverseGeoCodeSearchOption
- (BOOL)reverseGeoCode:(BMKReverseGeoCodeSearchOption*)reverseGeoCodeOption;
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher
result:(BMKReverseGeoCodeSearchResult *)result
errorCode:(BMKSearchErrorCode)error;
/** 添加BMKJumpingView. */
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation {
if ([annotation isEqual:self.annotation]) {
if (!_annotationView) {
_annotationView = [[BMKJumpingView alloc] initWithAnnotation:annotation reuseIdentifier:@"com.Baidu.BMKJumpingView"];
}
return _annotationView;
}
return nil;
}
/** 在地图区域即将改变时,标注调用jump动画.*/
- (void)mapView:(BMKMapView *)mapView regionWillChangeAnimated:(BOOL)animated reason:(BMKRegionChangeReason)reason {
// 大头针跳起动画
[self.annotationView jump];
}
/** 在地图区域改变完成后,标注调用fall动画并发起逆地理编码服务. */
- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated reason:(BMKRegionChangeReason)reason {
// 大头针落下动画
[self.annotationView fall];
// 选中点逆地理编码
[self pickUpPointReverseGeoCode:_annotation.coordinate];
}
/** 根据地图上的选点进行逆地理编码. */
- (void)pickUpPointReverseGeoCode:(CLLocationCoordinate2D)coordinate {
// 初始化BMKGeoCodeSearch实例
BMKGeoCodeSearch *geoCodeSearch = [[BMKGeoCodeSearch alloc]init];
// 设置反地理编码检索的代理
geoCodeSearch.delegate = self;
// 初始化请求参数类BMKReverseGeoCodeOption的实例
BMKReverseGeoCodeSearchOption *reverseGeoCodeOption = [[BMKReverseGeoCodeSearchOption alloc] init];
// 待解析的经纬度坐标(必选)
reverseGeoCodeOption.location = coordinate;
// 是否访问最新版行政区划数据(仅对中国数据生效)
reverseGeoCodeOption.isLatestAdmin = YES;
BOOL flag = [geoCodeSearch reverseGeoCode:reverseGeoCodeOption];
if (flag) {
NSLog(@"反地理编码检索成功");
} else {
NSLog(@"反地理编码检索失败");
}
}
/** 逆地理编码检索结果回调. */
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeSearchResult *)result errorCode:(BMKSearchErrorCode)error {
// 逆地理编码地址
_addressName = result.address;
// 结合当前位置POI的语义化结果描述
_detailDddress = result.sematicDescription;
// 当前位置的poi信息
self.poiList = [result.poiList copy];
__weak typeof(self) weakSelf = self;
// 刷新tableView更新数据
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.poiTableView reloadData];
});
}
复制成功