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
阅读全文
0 0
- iOS获取高德地图实现持续定位功能
- 利用高德地图实现定位功能
- 高德地图实战:后台持续定位实现
- 高德地图定位功能
- 高德地图定位功能
- ios高德地图提醒打开定位功能
- 高德地图获取定位
- 高德地图的定位功能的实现
- Android 高德地图定位功能的实现步骤
- android高德地图定位功能的实现
- 实现高德地图定位
- 高德地图实现定位
- 【iOS】高德地图MAMapKit的使用:地图显示、添加大头针、导航、定位功能介绍
- 高德地图定位功能(笔记)
- 高德地图定位和附近功能
- Android之高德地图定位SDK集成及地图功能实现
- android+高德地图实现基本地图和基本定位功能
- android 高德地图定位获取经纬度
- 斐波那契数列
- shell数值计算(加减乘除)
- 跳台阶
- python之 模块与包
- Golang中array和slice的总结
- iOS获取高德地图实现持续定位功能
- Java| 调试JDK源码
- Git团队协作使用规范
- 变态跳台阶
- PSA,为基于MCU的连接设备奠定基于硬件和固件的安全基础
- 矩形覆盖
- 三个数的最大值的求解
- idea 如何实现热部署,快捷便利
- 缺省配置Springboot Web应用启动中准备DispatchServlet