任务:显示地图在界面上并成功定位
一:我们导入高德地图的API
在Podfile
platform :ios, '7.0'
target ‘你的项目名称’ do
pod 'AMap3DMap'
pod ‘AMapSearch’
pod ‘AMapLocation’
end
注意:target中 正确输入你自己的项目名称
cd 进项目文件夹 pod install 成功后进入下一步。
二:加载地图视图
在.h文件中
#import
#import
#import
@interface MainPageVC : UIViewController
@property (nonatomic,strong)MAMapView *mapView;
@end
在.m文件中
- (void)ViewDidLoad{
[AMapServices sharedServices].enableHTTPS = YES;
_mapView = [[MAMapView alloc]initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height + [[UIApplication sharedApplication]statusBarFrame].size.height, self.view.frame.size.width, self.view.frame.size.height - (self.navigationController.navigationBar.frame.size.height + [[UIApplication sharedApplication]statusBarFrame].size.height))];
//delegate
[_mapView setDelegate:self];
//是否显示指南针
[_mapView setShowsCompass:YES];
//是否显示比例尺
[_mapView setShowsScale:YES];
//地图旋转
[_mapView setRotateEnabled:NO];
//地图倾斜
[_mapView setRotateCameraEnabled:NO];
//设置缩放级别
[_mapView setZoomLevel:15];
//是否显示用户位置
_mapView.showsUserLocation = YES;
//设置导航模式为跟随
_mapView.userTrackingMode = MAUserTrackingModeFollow ;
[self.view addSubview:_mapView];
}
至此,能够正确显示mapView了(不过显示的是北京的或者是你模拟器设置的坐标点的地方地图)
三:定位
3.1
获取key值(从这个网页里获得):点击打开链接
http://lbs.amap.com/api/ios-location-sdk/guide/create-project/get-key
AppDelegate.m 中添加设置key值
#import
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[AMapServices sharedServices].apiKey = @"987e1c858fb578f98b3c39e9516ecf96";
return YES;
}
3.2
给定位权限:
在info.plist中添加这三个权限
Privacy - Location Always and When In Use Usage Description
Privacy - Location Always Usage Description
Privacy - Location When In Use Usage Description
在App Transport Security Settings 中添加Allow Arbitrary Loads 为 YES
其后设置
3.3
#import
- (void)location{
if([CLLocationManager locationServicesEnabled]){
AMapLocationManager *locationManager = [[AMapLocationManager alloc]init];
[locationManager setDelegate:self];
//是否允许后台定位。默认为NO。只在iOS 9.0及之后起作用。设置为YES的时候必须保证 Background Modes 中的 Location updates 处于选中状态,否则会抛出异常。由于iOS系统限制,需要在定位未开始之前或定位停止之后,修改该属性的值才会有效果。
[locationManager setAllowsBackgroundLocationUpdates:NO];
//指定定位是否会被系统自动暂停。默认为NO。
[locationManager setPausesLocationUpdatesAutomatically:NO];
//设定定位的最小更新距离。单位米,默认为 kCLDistanceFilterNone,表示只要检测到设备位置发生变化就会更新位置信息
[locationManager setDistanceFilter:20];
//设定期望的定位精度。单位米,默认为 kCLLocationAccuracyBest
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
//开始定位服务
[locationManager startUpdatingLocation];
}
}