CoreLocation MKMapView

高德开发者平台 有开发指南

iOS9配置网络:

<key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>

请看这里  原文章:http://www.oschina.net/question/262659_149771?fromerr=Y0rzKueR

1. GPS定位:<CoreAudioKit/CoreAudioKit.h>

1. 基本属性:

1>

CLLocationManager:定位管理器   协议:<CLLocationManagerdelegate> 设置代理 实现方法

CLLocation:位置的具体信息(经纬度 等等)

CLHeading:设备移动方向

CLRegion:一个区域(常用子类:CLCircularRegion:圆形 CLBeaconRegion:蓝牙

[CLLocationManager locationServicesEnabled] 定位服务是否可用

distanceFilter:自动过滤距离 移动某个距离之后重新调用代理方法 更新位置

desiredAccuracy:定位的精度

self.manager.desiredAccuracy = kCLLocationAccuracyBest; // 最佳精度
self.manager.pausesLocationUpdatesAutomatically = YES; // 不需要的时候可以自动暂停
- (void)viewDidLoad {
    [super viewDidLoad];
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    
// 允许定位
    [self.locationManager requestAlwaysAuthorization];
    
// 自动过滤距离 移动100米之后重新调用代理方法 更新位置
    self.locationManager.distanceFilter = 100.0;  // 米为单位
    
// iOS7的系统下 写完start就可以开始定位了
    [self.locationManager startUpdatingLocation];
    
// 初始化地理编码器:
    self.geocoder = [CLGeocoder new];
}

2> CLGeocoder 地理编码器:

创建:

self.geocoder = [CLGeocoder new];

编码:提供某个字符串 来定位位置:- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;

[self.geocoder geocodeAddressString:self.inputLocation.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        // 取出一个位置信息
        CLPlacemark *placeMark = placemarks.lastObject;
        // 输出信息
        NSLog(@"%lf   %lf", placeMark.location.coordinate.latitude, placeMark.location.coordinate.longitude);
    }];

反编码:根据位置显示该地方的名字等等

[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        CLPlacemark *place = placemarks.lastObject;
        self.inputLocation.text = place.name;
        NSLog(@" %@",place.name);
    }];

2. 获取位置信息:

iOS7的系统下 写完start就可以开始定位了:

[self.locationManager startUpdatingLocation];

但是在iOS之后就需要设置是否允许定位:设置完成这个之后才可以定位

requestAlwaysAuthorization:一直允许定位

requestWhenInUseAuthorization:用户允许

在添加之前需要在info.plist 文件中添加字段:NSLocationAlwaysUsageDescription  (后面的字符串知识提示的时候会显示 并没有什么用)

[self.locationManager requestAlwaysAuthorization];

2. 地图:<MapKit/MapKit.h>

1> iOS原生地图:

前面带MK的是系统自带的地图:

MKUserLocation:地图上的大头针 有title subtitle等属性

MKMapView:用来显示地图 与视图一样 初始化需要确定frame 定位的时候需要用到coreLocation框架

showsUserLocation 设置为YES 允许跟踪定位 (MKMapView的属性)

可自定义MKAnnotation

//     创建比例系数 显示在某个点上
    MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.coordinate, MKCoordinateSpanMake(0.1, 0.1)) ;
//     比例系数越小 放大效果越大
    self.mapView.region = region; // 系统自带

2> 高德:

多以MA开头的:

[_mapView setZoomLevel:16.0 animated:YES];  设置缩放的比例

// 1. 验证key
    [MAMapServices sharedServices].apiKey = @“申请的key”;
    // 2. 初始化
    mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.bounds))];
    mapView.delegate = self;
    //    mapView.language = MAMapLanguageEn; // 设置地图显示语言
    mapView.mapType = MAMapTypeStandard; // 地图类型
    /*
     MAMapTypeSatellite:卫星地图
     MAMapTypeStandard:标准地图
     */
//    mapView.showTraffic = YES; // 显示实时交通路况
    [self.view addSubview:mapView];
    mapView.showsUserLocation = YES;

mapView的定位模式: userTrackingMode

MAUserTrackingModeNone:不跟随用户位置,仅在地图上显示。

MAUserTrackingModeFollow:跟随用户位置移动,并将定位点设置成地图中心点

MAUserTrackingModeFollowWithHeading:跟随用户的位置和角度移动

系统的地图和 高德地图 的区别:http://www.mamicode.com/info-detail-573898.html

原文地址:https://www.cnblogs.com/Evelyn-zn/p/4979101.html