ios 百度地图定位

本文介绍了一个基于iOS的地图应用实现过程,包括地图展示、用户定位、地理编码及逆地理编码等功能。通过实例展示了如何使用各类地图SDK进行开发,如设置地图中心、调整显示范围等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. #import "ViewController.h"  
  2. #import "UIView+Extension.h"  
  3. #import //引入base相关所有的头文件  
  4.   
  5. #import //引入地图功能所有的头文件  
  6.   
  7. #import //引入检索功能所有的头文件  
  8.   
  9. #import //引入云检索功能所有的头文件  
  10.   
  11. #import //引入定位功能所有的头文件  
  12.   
  13. #import //引入计算工具所有的头文件  
  14.   
  15. #import //引入周边雷达功能所有的头文件  
  16.   
  17. #import //只引入所需的单个头文件  
  18.   
  19. @interface ViewController ()  
  20.  
  21.     BMKPinAnnotationView *newAnnotation;  
  22.       
  23.     BMKGeoCodeSearch *_geoCodeSearch;  
  24.       
  25.     BMKReverseGeoCodeOption *_reverseGeoCodeOption;  
  26.       
  27.       
  28.  
  29. @property (nonatomic, strong) BMKLocationService *localService;  
  30. @property (weak, nonatomic) UIButton *mapPin;  
  31. @end  
  32.   
  33. @implementation ViewController  
  34. -(BMKLocationService *)localService{  
  35.     if (!_localService)  
  36.         _localService [[BMKLocationService alloc] init];  
  37.         [_localService setDesiredAccuracy:kCLLocationAccuracyBest];//设置定位精度  
  38.      
  39.     return _localService;  
  40.  
  41. (void)viewDidLoad  
  42.     [super viewDidLoad];  
  43.      
  44.     self.mapView [[BMKMapView alloc] initWithFrame:self.view.frame];  
  45.     self.mapPin [UIButton buttonWithType:UIButtonTypeSystem];//大头针  
  46.     self.mapPin.width 40;  
  47.     self.mapPin.height 80;  
  48.     self.mapPin.center self.mapView.center;  
  49.     [self.mapPin setBackgroundImage:[UIImage imageNamed:@"serach_Map"] forState:UIControlStateNormal];  
  50.     self.mapPin.backgroundColor [UIColor greenColor];  
  51.     [self.mapView addSubview:self.mapPin];  
  52.     [self.view addSubview:self.mapView];  
  53.     self.mapView.zoomLevel=17;//比例尺  
  54.     [self.mapView setMapType:BMKMapTypeStandard];//地图类型  
  55.     self.mapView.delegate self;  
  56.     self.mapView.userTrackingMode BMKUserTrackingModeFollow;//设置定位的状态  
  57.     self.mapView.showsUserLocation YES;//显示定位图层  
  58.     self.localService.delegate self;  
  59.     [self.localService startUserLocationService];//用户开始定位  
  60.       
  61.     //self.mapView.showsUserLocation NO;//先关闭显示的定位图层  
  62.     self.mapView.userTrackingMode BMKUserTrackingModeFollow;//设置定位的状态  
  63.     self.mapView.showsUserLocation YES;//显示定位图层  
  64.     [self.mapView bringSubviewToFront:self.mapPin];  
  65.   
  66.   
  67.  
  68. #pragma mark -- BMKLocationServiceDelegate  
  69. (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation  
  70.  
  71.     self.mapView.showsUserLocation YES;//显示定位图层  
  72.     //设置地图中心为用户经纬度  
  73.     [self.mapView updateLocationData:userLocation];  
  74.       
  75.       
  76.     //    _mapView.centerCoordinate userLocation.location.coordinate;  
  77.     BMKCoordinateRegion region ;//表示范围的结构体  
  78.     region.center self.mapView.centerCoordinate;//中心点  
  79.     region.span.latitudeDelta 0.004;//经度范围(设置为0.1表示显示范围为0.2的纬度范围)  
  80.     region.span.longitudeDelta 0.004;//纬度范围  
  81.     [self.mapView setRegion:region animated:YES];  
  82.       
  83.  
  84. #pragma mark -- BMKMapViewDelegate  
  85. (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated  
  86.  
  87.     //屏幕坐标转地图经纬度  
  88.     CLLocationCoordinate2D MapCoordinate=[_mapView convertPoint:_mapPin.center toCoordinateFromView:_mapView];  
  89.     NSLog(@"latitude == %f longitude == %f",MapCoordinate.latitude,MapCoordinate.longitude);  
  90.     if (_geoCodeSearch==nil)  
  91.         //初始化地理编码类  
  92.         _geoCodeSearch [[BMKGeoCodeSearch alloc]init];  
  93.         _geoCodeSearch.delegate self;  
  94.           
  95.      
  96.     if (_reverseGeoCodeOption==nil)  
  97.           
  98.         //初始化反地理编码类  
  99.         _reverseGeoCodeOption= [[BMKReverseGeoCodeOption alloc] init];  
  100.      
  101.       
  102.     //需要逆地理编码的坐标位置  
  103.     _reverseGeoCodeOption.reverseGeoPoint =MapCoordinate;  
  104.     [_geoCodeSearch reverseGeoCode:_reverseGeoCodeOption];  
  105.       
  106.     //创建地理编码对象  
  107.     CLGeocoder *geocoder=[[CLGeocoder alloc]init];  
  108.     //创建位置  
  109.     CLLocation *location=[[CLLocation alloc]initWithLatitude:MapCoordinate.latitude longitude:MapCoordinate.longitude];  
  110.       
  111.     //反地理编码  
  112.     [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<</span>CLPlacemark *> _Nullable placemarks, NSError _Nullable error)  
  113.         //判断是否有错误或者placemarks是否为空  
  114.         if (error !=nil || placemarks.count==0)  
  115.             NSLog(@"%@",error);  
  116.             return  
  117.          
  118.         for (CLPlacemark *placemark in placemarks)  
  119.             //赋值详细地址  
  120.             NSLog(@"%@",placemark.name);  
  121.          
  122.     }];  
  123.  
  124. #pragma mark -- BMKGeoCodeSearchDelegate  
  125. //周边信息  
  126. (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{  
  127.     for (BMKPoiInfo *poi in result.poiList)  
  128.         NSLog(@"%@",poi.name);//周边建筑名  
  129.         NSLog(@"%d",poi.epoitype);  
  130.           
  131.      
  132.  
  133. @end 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值