iOS百度地圖簡單應用( iOS地圖定位(定位、地理編碼與反地理編碼、mapView、大頭針)
阿新 • • 發佈:2019-01-30
匯入百度SDK
注:自iOS8起,系統定位功能進行了升級,SDK為了實現最新的適配,自v2.5.0起也做了相應的修改,開發者在使用定位功能之前,需要在info.plist裡新增(以下二選一,兩個都新增預設使用NSLocationWhenInUseUsageDescription):
NSLocationWhenInUseUsageDescription ,允許在前臺使用時獲取GPS的描述
NSLocationAlwaysUsageDescription ,允許永久使用GPS的描述
下面為核心程式碼。
效果圖// // ViewController.m // SwalleMap // // Created by dev on 16/6/1. // Copyright © 2016年 SWALLE. All rights reserved. // #import "ViewController.h" #import "UIView+Extension.h" #import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相關所有的標頭檔案 #import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地圖功能所有的標頭檔案 #import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入檢索功能所有的標頭檔案 #import <BaiduMapAPI_Cloud/BMKCloudSearchComponent.h>//引入雲檢索功能所有的標頭檔案 #import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的標頭檔案 #import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//引入計算工具所有的標頭檔案 #import <BaiduMapAPI_Radar/BMKRadarComponent.h>//引入周邊雷達功能所有的標頭檔案 #import <BaiduMapAPI_Map/BMKMapView.h>//只引入所需的單個頭檔案 @interface ViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate> { 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) { //初始化反地理編碼類 _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<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