您的位置:首页 > 移动开发 > IOS开发

ios 百度地图定位

2017-05-13 18:11 357 查看
#import "ViewController.h"  

#import "UIView+Extension.h"  

#import //引入base相关所有的头文件  

  

#import //引入地图功能所有的头文件  

  

#import //引入检索功能所有的头文件  

  

#import //引入云检索功能所有的头文件  

  

#import //引入定位功能所有的头文件  

  

#import //引入计算工具所有的头文件  

  

#import //引入周边雷达功能所有的头文件  

  

#import //只引入所需的单个头文件  

  

@interface ViewController ()  

{  

    BMKPinAnnotationView *newAnnotation;  

      

    BMKGeoCodeSearch *_geoCodeSearch;  

      

    BMKReverseGeoCodeOption *_reverseGeoCodeOption;  

      

      

}  

@property (nonatomic, strong) BMKLocationService *localService;  

@property (weak, nonatomic) UIButton *mapPin;  

@end  

  

@implementation ViewController  

-(BMKLocationService *)localService{  

    if (!_localService) {  

        _localService = [[BMKLocationService alloc] init];  

        [_localService setDesiredAccuracy:kCLLocationAccuracyBest];//设置定位精度  

    }  

    return _localService;  

}  

- (void)viewDidLoad {  

    [super viewDidLoad];  

     

    self.mapView = [[BMKMapView alloc] initWithFrame:self.view.frame];  

    self.mapPin = [UIButton buttonWithType:UIButtonTypeSystem];//大头针  

    self.mapPin.width = 40;  

    self.mapPin.height = 80;  

    self.mapPin.center = self.mapView.center;  

    [self.mapPin setBackgroundImage:[UIImage imageNamed:@"serach_Map"] forState:UIControlStateNormal];  

    self.mapPin.backgroundColor = [UIColor greenColor];  

    [self.mapView addSubview:self.mapPin];  

    [self.view addSubview:self.mapView];  

    self.mapView.zoomLevel=17;//比例尺  

    [self.mapView setMapType:BMKMapTypeStandard];//地图类型  

    self.mapView.delegate = self;  

    self.mapView.userTrackingMode = BMKUserTrackingModeFollow;//设置定位的状态  

    self.mapView.showsUserLocation = YES;//显示定位图层  

    self.localService.delegate = self;  

    [self.localService startUserLocationService];//用户开始定位  

      

    //self.mapView.showsUserLocation = NO;//先关闭显示的定位图层  

    self.mapView.userTrackingMode = BMKUserTrackingModeFollow;//设置定位的状态  

    self.mapView.showsUserLocation = YES;//显示定位图层  

    [self.mapView bringSubviewToFront:self.mapPin];  

  

  

}  

#pragma mark -- BMKLocationServiceDelegate  

- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation  

{  

    self.mapView.showsUserLocation = YES;//显示定位图层  

    //设置地图中心为用户经纬度  

    [self.mapView updateLocationData:userLocation];  

      

      

    //    _mapView.centerCoordinate = userLocation.location.coordinate;  

    BMKCoordinateRegion region ;//表示范围的结构体  

    region.center = self.mapView.centerCoordinate;//中心点  

    region.span.latitudeDelta = 0.004;//经度范围(设置为0.1表示显示范围为0.2的纬度范围)  

    region.span.longitudeDelta = 0.004;//纬度范围  

    [self.mapView setRegion:region animated:YES];  

      

}  

#pragma mark -- BMKMapViewDelegate  

- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated  

{  

    //屏幕坐标转地图经纬度  

    CLLocationCoordinate2D MapCoordinate=[_mapView convertPoint:_mapPin.center toCoordinateFromView:_mapView];  

    NSLog(@"latitude == %f longitude == %f",MapCoordinate.latitude,MapCoordinate.longitude);  

    if (_geoCodeSearch==nil) {  

        //初始化地理编码类  

        _geoCodeSearch = [[BMKGeoCodeSearch alloc]init];  

        _geoCodeSearch.delegate = self;  

          

    }  

    if (_reverseGeoCodeOption==nil) {  

          

 
bcf1
       //初始化反地理编码类  

        _reverseGeoCodeOption= [[BMKReverseGeoCodeOption alloc] init];  

    }  

      

    //需要逆地理编码的坐标位置  

    _reverseGeoCodeOption.reverseGeoPoint =MapCoordinate;  

    [_geoCodeSearch reverseGeoCode:_reverseGeoCodeOption];  

      

    //创建地理编码对象  

    CLGeocoder *geocoder=[[CLGeocoder alloc]init];  

    //创建位置  

    CLLocation *location=[[CLLocation alloc]initWithLatitude:MapCoordinate.latitude longitude:MapCoordinate.longitude];  

      

    //反地理编码  

    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<</span>CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {  

        //判断是否有错误或者placemarks是否为空  

        if (error !=nil || placemarks.count==0) {  

            NSLog(@"%@",error);  

            return ;  

        }  

        for (CLPlacemark *placemark in placemarks) {  

            //赋值详细地址  

            NSLog(@"%@",placemark.name);  

        }  

    }];  

}  

#pragma mark -- BMKGeoCodeSearchDelegate  

//周边信息  

- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{  

    for (BMKPoiInfo *poi in result.poiList) {  

        NSLog(@"%@",poi.name);//周边建筑名  

        NSLog(@"%d",poi.epoitype);  

          

    }  

}  

@end 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: