iOS获取高德地图实现持续定位功能

来源:互联网 发布:淘宝开店铺要交押金吗 编辑:程序博客网 时间:2024/05/16 04:58

首先,根据高德地图开发平台在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

原创粉丝点击