一、定位步骤

1.Xcode自带地图,直接先引入头文件

#import <CoreLocation/CoreLocation.h>

2.CLLocation框架中的CLLocationManager用于管理定位的管理器

//CLLocation框架中的CLLocationManager用于管理定位的管理器
@property (nonatomic, strong)CLLocationManager *manager;

初始化定位管理器:self.manager = [[CLLocationManager alloc] init];

3(1)进行隐私判断

    if ([CLLocationManager locationServicesEnabled]) {
NSLog(@"是否前往隐私进行设置允许定位");//此处自己设置提示框
}

3(2)并授权

    if ([[[UIDevice currentDevice] systemVersion] integerValue] >= 8.0) {       //进行版本的判断
/*定位服务授权状态,返回枚举类型:
kCLAuthorizationStatusNotDetermined: 用户尚未做出决定是否启用定位服务
kCLAuthorizationStatusRestricted: 没有获得用户授权使用定位服务,可能用户没有自己禁止访问授权
kCLAuthorizationStatusDenied :用户已经明确禁止应用使用定位服务或者当前系统定位服务处于关闭状态
kCLAuthorizationStatusAuthorizedAlways: 应用获得授权可以一直使用定位服务,即使应用不在使用状态
kCLAuthorizationStatusAuthorizedWhenInUse: 使用此应用过程中允许访问定位服务
*/
if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) {
//在授权请求之前需要在info.plist文件中设置允许定位的内容:NSLocationWhenInUseUsageDescription,在NSLocationWhenInUseUsageDescription后面的values值上添加提示,该提示会显示在请求授权时弹出的提示框上
//请求授权
[self.manager requestWhenInUseAuthorization];
}
}

4.设置管理器的代理和相关属性

    self.manager.delegate = self;
//设置精度
self.manager.desiredAccuracy = ;
//设置最小更新距离
self.manager.distanceFilter = ; //开启定位
[self.manager startUpdatingLocation]

5.CLLocation框架中的CLGeocoder能进行地理位置的编码和反编码

//CLLocation框架中的CLGeocoder能进行地理位置的编码和反编码
@property (nonatomic, strong) CLGeocoder *geocoder;

6.定位的代理方法

//这个代理方法是定位成功之后开始更新位置信息,只要移动设置的最小距离之后就会自动开始走这个方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations { //获取最后一次的位置
CLLocation *location = locations.lastObject;
//获取位置
CLLocationCoordinate2D coordinate = location.coordinate; NSLog(@"经度:%f, 纬度:%f, 海拔:%f, 航向:%f, 行走速度:%f", coordinate.longitude, coordinate.latitude, location.altitude, location.course, location.speed); //longitude经度,latitude纬度,location.altitude海拔,location.course航海方向,location.speed速度 //为了节省电源,如果不使用定位,需要把定位关掉
[self.manager stopUpdatingLocation]; } //定位失败方法
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"定位失败");
}

实现定位

二、根据地名编码获取经纬度输入地名自动联网获取该地名对应的所有位置

#pragma mark - 根据地名编码获取相关信息
- (void)getCoordinateByAddress:(NSString *)address { //编码方法
[_geocoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
/* addressDictionary中包括的信息有:
// NSString *name=placemark.name;//地名
// NSString *thoroughfare=placemark.thoroughfare;//街道
// NSString *subThoroughfare=placemark.subThoroughfare; //街道相关信息,例如门牌等
// NSString *locality=placemark.locality; // 城市
// NSString *subLocality=placemark.subLocality; // 城市相关信息,例如标志性建筑
// NSString *administrativeArea=placemark.administrativeArea; // 州
// NSString *subAdministrativeArea=placemark.subAdministrativeArea; //其他行政区域信息
// NSString *postalCode=placemark.postalCode; //邮编
// NSString *ISOcountryCode=placemark.ISOcountryCode; //国家编码
// NSString *country=placemark.country; //国家
// NSString *inlandWater=placemark.inlandWater; //水源、湖泊
// NSString *ocean=placemark.ocean; // 海洋
// NSArray *areasOfInterest=placemark.areasOfInterest; //关联的或利益相关的地标
*/
//根据返回的地标,取出第一个位置(地标的位置很多)
CLPlacemark *mark = placemarks.firstObject; //根据地标得到location
CLLocation *location = mark.location; //根据location获取区域
CLRegion *region = mark.region; //获取字典信息
NSDictionary *addressDic = mark.addressDictionary; NSLog(@"地标:%@, 区域:%@, 字典-地理位置信息(包括街道、地名...)%@",location, region, addressDic); }]; }

地名获取经纬度方法

二、三两个方法不能在一个viewDidLoad里顺序执行,只会执行第一个不执行第二个,或者延时一段时间后,再执行第二个,建议添加两个按钮分别执行这两个方法。

三、根据经纬度反编码取出地名

输入坐标经纬度获取该位置地理信息

- (void)getAddressByLatitude:(CLLocationDegrees)latitude
Longitude:(CLLocationDegrees)longitude { //反编码
//创建CLLocation
CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude]; [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { NSDictionary *dic = placemarks.firstObject.addressDictionary;
NSLog(@"反编码地理位置信息:%@",dic); }]; }

经纬度获取地名方法

四、计算两点之间的距离
输入两个坐标,获得两坐标之间的距离

- (void)distance {

    /*
北京:N40,E116
大连:N39,E121
*/ //创建位置1
CLLocation *locationBeiJing = [[CLLocation alloc] initWithLatitude: longitude:];
//创建位置2
CLLocation *locationDaLian = [[CLLocation alloc] initWithLatitude: longitude:]; //距离计算方法
CLLocationDistance distance = [locationBeiJing distanceFromLocation:locationDaLian]; NSLog(@"北京到大连的距离为:%f", distance); }

获取距离方法

iOS进阶_地图定位的更多相关文章

  1. iOS进阶_地图上定位的标志——大头针

    一.添加大头针 地图使用的框架是MapKit 大头针走的是MKAnnotation协议 /* 注意:因为是满足协议MKAnnotation,所以没有MKAnnotation的系统大头针类,必须自定义大 ...

  2. ios调用百度地图定位遇到的奇葩问题

    app项目过程中需要用到百度地图,然后网上可以查资料看官网文档,最后弄了好几天还是不行,找了各位前辈帮忙虽然解决了,但是把代码拷贝到我的项目时又无法定位了,最后查看了下原因是info配置出了问题,不是 ...

  3. iOS进阶_三方使用步骤

    一.配置环境(:后为在终端输入的命令) 打开终端 查看自己电脑的Ruby环境:gem sources -l 如果环境已经是淘宝镜像了,此时不需要再进行环境的修改. 如果不是,发送gem sources ...

  4. iOS进阶_动画的多种实现方式

    一.UIView动画 //UIView动画有开始beginAnimation,有结束commitAnimation    //第一步:开始UIView动画    [UIView beginAnimat ...

  5. iOS 地图定位及大头针的基本使用

    地图 Part1 - 定位及大头针的基本使用 一.MapKit 作用 : 用于地图展示 如大头针,路线,覆盖层展示等(着重界面展示) 使用步骤 导入头文件 #import <MapKit/Map ...

  6. iOS开发系列--地图与定位

    概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.例如你到了一个 ...

  7. iOS中的地图和定位

    文章摘自http://www.cnblogs.com/kenshincui/p/4125570.html#location  如有侵权,请联系删除. 概览 现在很多社交.电商.团购应用都引入了地图和定 ...

  8. iOS | 地图定位

    在IOS开发中,最常见的功能之一就是地图定位功能,不单单是百度地图,高德地图等专业的地图导航软件,还有美团,咕咚等一些美食购物类和运动类也需要这样的功能,所以学会这项技能是一名IOS开发工程师必须的. ...

  9. IOS中使用百度地图定位后获取城市坐标,城市名称,城市编号信息

    IOS中使用百度地图定位后获取城市坐标,城市名称,城市编号信息 /**当获取到定位的坐标后,回调函数*/ - (void)didUpdateBMKUserLocation:(BMKUserLocati ...

随机推荐

  1. Reflector.exe 破解注意事项

    需要把网络断掉,然后选择手动激活 总结经验:操作步骤要仔细看清,否则会更浪费时间

  2. XML约束之DTD

    XML文件的约束:什么叫约束呢?顾名思义,就是对xml文件的内容进行按照既定规则的限制.我们知道,因为xml文件的标签是可以自定义的,而往往我们用xml文件都是为了表达一定的数据集合(即小型的数据库) ...

  3. JS绑定JavaScript事件

    //onblur="onblurs(this)" // function onblurs(e) { // alert(e.value); // }

  4. oracle 脱敏和加密

     脱敏:在此只是对数据如姓名,身份证号码等进行简单粗暴的脱敏,即改变数据 针对于number类型的数据:update table_name set conlunm_name =  substr (co ...

  5. 转载:Bootstrap之表格checkbox复选框全选

    转:http://blog.csdn.net/shangmingchao[商明超的博客] 效果图: HTML中无需添加额外的一列来表示复选框,而是由JS完成,所以正常的表格布局就行了: 版权声明:如需 ...

  6. ELK日志管理之——kibana部署

    1.kibana安装 [root@localhost ~]# wget https://download.elastic.co/kibana/kibana/kibana-4.1.1-linux-x64 ...

  7. Validate US Telephone Numbers

    function telephoneCheck(str) { // Good luck! //return true; var phone = /^1? ?(\d{3}|\(\d{3}\))[ -]? ...

  8. JS学习之函数内部属性和方法

    知识点:arguments和this对象.caller属性.apply()和call()方法     arguments对象:函数内部对象,传入函数中所有参数的集合,类数组对象 属性:callee 指 ...

  9. [z]Oracle性能优化-读懂执行计划

    http://blog.csdn.net/lifetragedy/article/details/51320192 Oracle的执行计划   得到执行计划的方式       Autotrace例子 ...

  10. 提高SQL查询效率(SQL优化)

    要提高SQL查询效率where语句条件的先后次序应如何写 http://blog.csdn.net/sforiz/article/details/5345359   我们要做到不但会写SQL,还要做到 ...