iOS獲取高德地圖實現持續定位功能
阿新 • • 發佈:2019-02-10
首先,根據高德地圖開發平臺在Xcode裡面配置相應的環境
自動部署用cocoapods,請按照http://lbs.amap.com/api/ios-location-sdk/guide/create-project/cocoapods
手動部署請按照http://lbs.amap.com/api/ios-location-sdk/guide/create-project/manual-configuration
配置好相應環境之後,開始實現持續定位功能,可參照http://lbs.amap.com/api/ios-location-sdk/guide/get-location/seriallocation
廢話不多說,開始上程式碼!
// // ViewController.m // 1207 // // Created by apple on 2017/12/7. // Copyright © 2017年 Aliya. All rights reserved. // #import "ViewController.h" #import <MAMapKit/MAMapKit.h> #import <AMapFoundationKit/AMapFoundationKit.h> #import <AMapLocationKit/AMapLocationKit.h> #define APIKey @"你的key" @interface ViewController ()<MAMapViewDelegate,AMapLocationManagerDelegate> { MAMapView *_mapView; AMapLocationManager *locationManager; } @property (nonatomic, strong) UISegmentedControl *showSegment; @property (nonatomic, strong) MAPointAnnotation *pointAnnotaiton; @end @implementation ViewController - (void)configLocationManager { locationManager = [[AMapLocationManager alloc] init]; [locationManager setDelegate:self]; //設定不允許系統暫停定位 [locationManager setPausesLocationUpdatesAutomatically:NO]; //設定允許在後臺定位 [locationManager setAllowsBackgroundLocationUpdates:YES]; //設定允許連續定位逆地理 [locationManager setLocatingWithReGeocode:YES]; } - (void)showsSegmentAction:(UISegmentedControl *)sender { if (sender.selectedSegmentIndex) { //停止定位 [locationManager stopUpdatingLocation]; //移除地圖上的annotation [_mapView removeAnnotations:_mapView.annotations]; self.pointAnnotaiton = nil; } else { //開始進行連續定位 [locationManager startUpdatingLocation]; } } #pragma mark - AMapLocationManager Delegate - (void)amapLocationManager:(AMapLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"%s, amapLocationManager = %@, error = %@", __func__, [manager class], error); } - (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode { NSLog(@"location:{lat:%f; lon:%f; accuracy:%f; reGeocode:%@}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy, reGeocode.formattedAddress); //獲取到定位資訊,更新annotation if (self.pointAnnotaiton == nil) { self.pointAnnotaiton = [[MAPointAnnotation alloc] init]; [self.pointAnnotaiton setCoordinate:location.coordinate]; [_mapView addAnnotation:self.pointAnnotaiton]; } [self.pointAnnotaiton setCoordinate:location.coordinate]; [_mapView setCenterCoordinate:location.coordinate]; [_mapView setZoomLevel:15.1 animated:NO]; } #pragma mark - Initialization - (void)initMapView { if (_mapView == nil) { _mapView = [[MAMapView alloc] initWithFrame:self.view.bounds]; [_mapView setDelegate:self]; [self.view addSubview:_mapView]; } } - (void)initBar { UIView *barView =[[UIView alloc]initWithFrame:CGRectMake(0, self.view.bounds.size.height-40, self.view.bounds.size.width, 40)]; barView.backgroundColor =[UIColor whiteColor]; [self.view addSubview:barView]; UISegmentedControl* showSegment =[[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"Start",@"Stop" ,nil]]; [showSegment addTarget:self action:@selector(showsSegmentAction:) forControlEvents:UIControlEventValueChanged]; showSegment.selectedSegmentIndex = 0; [barView addSubview:showSegment]; showSegment.frame =CGRectMake(self.view.center.x-35, 5, 80, 30); } #pragma mark - Life Cycle - (void)viewDidLoad { [super viewDidLoad]; [AMapServices sharedServices].apiKey =APIKey; [self.view setBackgroundColor:[UIColor whiteColor]]; [self initMapView]; [self configLocationManager]; [self initBar]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [locationManager startUpdatingLocation]; } #pragma mark - MAMapView Delegate - (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation { if ([annotation isKindOfClass:[MAPointAnnotation class]]) { static NSString *pointReuseIndetifier = @"pointReuseIndetifier"; MAPinAnnotationView *annotationView = (MAPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier]; if (annotationView == nil) { annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndetifier]; } annotationView.canShowCallout = NO; annotationView.animatesDrop = NO; annotationView.draggable = NO; annotationView.image = [UIImage imageNamed:@"icon_location.png"]; return annotationView; } return nil; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
注:模擬器上看不到具體位置,只有在真機上才能看到位置
下載地址 http://download.csdn.net/download/elegentbeauty/10161278