0.使用百度地图,点击屏幕下设置标注,并显示出当前标注的地区地址
1.项目工程文件设置参考百度SDK
2.在百度地图显示页面
#pragma mark -懒加载
//地图
- (BMKMapView *)mapView
{
if (!_mapView) {
_mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, -64, kWeight, kHeight)];
self.displayParam = [[BMKLocationViewDisplayParam alloc]init];
_displayParam.isRotateAngleValid = YES;//跟随态旋转角度是否生效
_displayParam.isAccuracyCircleShow = NO;//精度圈是否显示
//_displayParam.locationViewImgName= @"icon";//定位图标名称
_displayParam.locationViewOffsetX = 0;//定位偏移量(经度)
_displayParam.locationViewOffsetY = 0;//定位偏移量(纬度)
[_mapView updateLocationViewWithParam:_displayParam];
}
return _mapView;
}
//地图搜索
- (BMKGeoCodeSearch *)geocodesearch
{
if (!_geocodesearch) {
_geocodesearch = [[BMKGeoCodeSearch alloc] init];
_geocodesearch.delegate = self;
}
return _geocodesearch;
}
//地图标注
- (BMKPointAnnotation *)annotation
{
if (!_annotation) {
_annotation =[[BMKPointAnnotation alloc]init];
}
return _annotation;
}
- (void)viewWillAppear:(BOOL)animated
{
[_mapView viewWillAppear];
_mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
}
- (void)viewWillDisappear:(BOOL)animated
{
[_mapView viewWillDisappear];
_mapView.delegate = nil; // 不用时,置nil
}
#pragma mark -viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
//添加地图View
[self.view addSubview:self.mapView];
//开启定位服务
self.locService = [[BMKLocationService alloc] init];
self.locService.delegate = self;
//启动LocationService
[self.locService startUserLocationService];
//初始化地图显示位置 上海
[self setUpLocal];
//设定是否显示定位图层 及 定位精确度
_mapView.showsUserLocation = YES;//显示定位图层
_mapView.userTrackingMode = BMKUserTrackingModeNone;
//添加位置显示框
[self addLocationLabel];
//添加确定按钮
[self addDoneButton];
}
//初始化地图显示在上海
- (void)setUpLocal
{
BMKCoordinateRegion region ;//表示范围的结构体
CLLocationCoordinate2D coor;
coor.latitude = 31.171321;
coor.longitude = 121.411398;
region.center = coor;//中心点
region.span.latitudeDelta = 0.1;//经度范围(设置为0.1表示显示范围为0.2的纬度范围)
region.span.longitudeDelta = 0.1;//纬度范围
[_mapView setRegion:region animated:YES];
}
//添加位置显示框
- (void)addLocationLabel
{
UILabel *locationLabel = [[UILabel alloc] init];
locationLabel.backgroundColor = [UIColor whiteColor];
locationLabel.text = @"寄件地址: ";
locationLabel.frame = CGRectMake(20, 25, AppWeight-40, 30);
locationLabel.numberOfLines = 0;
locationLabel.font = [UIFont systemFontOfSize:12];
// 把label放在最顶层
[self.view insertSubview:locationLabel aboveSubview:self.mapView];
self.locationLabel = locationLabel;
}
//添加确定按钮
- (void)addDoneButton
{
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.doneButton = doneButton;
doneButton.frame = CGRectMake(30, AppHeight - 120, AppWeight-60, 45);
[doneButton setBackgroundColor:[UIColor orangeColor]];
[doneButton setTitle:@"确 定" forState:UIControlStateNormal];
[doneButton setTitle:@"确 定" forState:UIControlStateHighlighted];
[doneButton addTarget:self action:@selector(doneButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:doneButton];
}
//完成按钮 把当前标注的地址传给上一个控制器的textField
- (void)doneButtonClick:(UIButton *)button
{
NSString *str = self.locationLabel.text;
str = [str substringFromIndex:6];
if (_localStringBlock) {
_localStringBlock(str);
}
[self.navigationController popViewControllerAnimated:YES];
}
# pragma mark -BMKMapViewDelegate
//点击屏幕 添加标注
- (void)mapView:(BMKMapView *)mapView onClickedMapBlank:(CLLocationCoordinate2D)coordinate
{
//拿到点击的经纬度 然后添加标
CLLocationCoordinate2D coor;
coor.latitude = coordinate.latitude;
coor.longitude = coordinate.longitude;
self.annotation.coordinate = coor;
[_mapView addAnnotation:self.annotation];
[self.mapView setCenterCoordinate:coor animated:YES];
///反geo检索信息类
BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc] init];
reverseGeocodeSearchOption.reverseGeoPoint = coor;
BOOL flag = [self.geocodesearch reverseGeoCode:reverseGeocodeSearchOption];;
if (flag) {
NSLog(@"反地理编码成功");
}else {
NSLog(@"反地理编码失败");
}
}
//根据anntation生成对应的view
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation
{
NSString *AnnotationViewID = @"renameMark";
BMKPinAnnotationView *annotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil) {
annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
annotationView.image = [UIImage imageNamed:@"地图-选中"];
}
return annotationView;
}
#pragma mark -BMKGeoCodeSearchDelegate
//根据 经纬度 获取 地区信息
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
if (error == BMK_SEARCH_NO_ERROR) {
// NSLog(@"%@",result.address);
// NSLog(@"%@",result.addressDetail);
self.locationLabel.text = [NSString stringWithFormat:@"寄件地址: %@",result.address];
}else {
NSLog(@"未找到结果");
}
}
/**
* 当选中一个annotation views时,调用此接口
* @param mapView 地图View
* @param views 选中的annotation views
*/
//- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view
//{
// _shopCoor = view.annotation.coordinate;
//}
///**
// * 选中气泡调用方法
// * @param mapView 地图
// * @param view annotation
// */
//- (void)mapView:(BMKMapView *)mapView annotationViewForBubble:(BMKAnnotationView *)view
//{
// MyBMKPointAnnotation *tt = (MyBMKPointAnnotation *)view.annotation;
// if (tt.shopID) {
// BusinessIfonUVC *BusinessIfonVC = [[BusinessIfonUVC alloc]init];
// BusinessIfonVC.shopId = tt.shopID;
// [self.navigationController pushViewController:BusinessIfonVC animated:YES];
// }
//}
#pragma mark -BMKLocationServiceDelegate
//处理方向变更信息
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
// NSLog(@"heading is %@",userLocation.heading);
[_mapView updateLocationData:userLocation];
}
//处理位置坐标更新 (定位到位置是,把位置显示到地图正中间,只执行一次)
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
// NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
//在地图上更新到用户所在位置 (只执行一次)
if (userLocation.location.coordinate.latitude != 0 || userLocation.location.coordinate.longitude != 0) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_mapView.centerCoordinate = userLocation.location.coordinate;
CLLocationCoordinate2D coor;
coor.latitude = userLocation.location.coordinate.latitude;
coor.longitude = userLocation.location.coordinate.longitude;
BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc] init];
reverseGeocodeSearchOption.reverseGeoPoint = coor;
BOOL flag = [self.geocodesearch reverseGeoCode:reverseGeocodeSearchOption];;
if (flag) {
NSLog(@"反地理编码成功1111111");
}else {
NSLog(@"反地理编码失败1111111");
}
});
}
[_mapView updateLocationData:userLocation];
}
map.jpg
网友评论