iOS地图定位小功能(上)

       讲到iOS的定位呢,首先先介绍一下定位这东西。iOS系统自带的定位服务可以实现很多需求。比如:获取当前经纬度,获取当前位置信息等等。

一、介绍

 1、定位 使用CoreLocation框架
    2、功能
     (1)基础定位
     (2)地理编码(把文字位置转换成地理位置编码) 反编码(把编码转换成文字位置)
    3、ios8 ios9之后的改变
     (1)添加了定位服务的目的
         《1》NSLocationAlwaysUsageDescription
         《2》NSLocationWhenInUseUsageDescription (写在info.plist里面,作为使用定位功能的提示文字 可以同时出现)
         《3》如果忘记写 就不能使用定位功能,并且没有任何提示信息
     (2)需要请求用户授权
          《1》requestAlwaysAuthorization
          《2》requestWhenInUseAuthorization
      (3)ios9 喊Home键进入后台 如果需要继续定位
          《1》需要在Info.plist文件里面添加Required background modes---->数组里面添加App registers for location updates   如果不添加这对键值,却使用后台定位功能会直接崩溃
          《2》在代码里面 allowsBackgroundLocationUpdates 需要将这个属性设置成YES,满足《1》、《2》才可以使用定位
  二、使用
    1、使用定位服务所需的相关类和其他数据类型
    (1)CLLocationManager  定位的管理者 可以通过这个类创建定位服务的功能
    (2)CLLocation  地理位置信息相关的类
    (3)CLLocationCoordinate2D  经纬度的一个信息类型 坐标的数据类型(结构体)
    (4)CLRegion  表示范围的类
    (5)CLGeocoder   地理编码  反地理编码的类
    (6)CLPlacemark  表示地标的类(用文字表示出来的) 里面包含了location
    (7)CLHeading  表示导航方向的类 航向
    (8)CLCircular
     (9)CLLocationDistance:距离
    2、具体使用
     (1)定位
          《0》检查用户是否在设置中 打开了定位服务
          《1》初始化定位对象
          《2》info中添加描述用使用定位的目的,并向用户申请授权
          《3》挂上代理  并实现代理方法
          《4》如果需要使用后台定位服务需要在 info中添加Required background modes 这个key 以及它里面的元素App registers for location updates
          《5》开始定位
     (2)地理编码 反编码
 
 CLLocation位置信息
      <1>coordinate:经纬度
         1、latitude 纬度
         2、longitude  经度
      <2>altitude:高度
      <3>horizontalAccuracy:水平的精准度,可以用它来检测是否定位成功
      <4>verticalAccuracy:垂直的精准度
      <5>course:0.0~359.9 真北方向为0.0
      <6>speed:速度
      <7>timestamp:时间戳
      <8>floor——>楼层信息
以上大致介绍完毕,下面要进行实战:

获取当前经纬度

首先导入#import <CoreLocation/CoreLocation.h>,定义CLLocationManager的实例,实现CLLocationManagerDelegate。

@interface ViewController ()<CLLocationManagerDelegate>
    {
        CLLocationManager *_locationManager;
    }
    
    @end

开始定位的方法:

- (void)startLocating
    {
        if([CLLocationManager locationServicesEnabled])
        {
            _locationManager = [[CLLocationManager alloc] init];
            //设置定位的精度
            [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
            _locationManager.distanceFilter = 100.0f;
            _locationManager.delegate = self;
            if ([[[UIDevice currentDevice] systemVersion] floatValue] > 8.0)
            {
                [_locationManager requestAlwaysAuthorization];
                [_locationManager requestWhenInUseAuthorization];
            }
            //开始实时定位
            [_locationManager startUpdatingLocation];
        }
    }
实现代理方法:
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
    {
        NSLog(@"Longitude = %f", manager.location.coordinate.longitude);
        NSLog(@"Latitude = %f", manager.location.coordinate.latitude);
        [_locationManager stopUpdatingLocation];
    }
获得当前的位置信息

在上面的代理

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
    {
        NSLog(@"Longitude = %f", manager.location.coordinate.longitude);
        NSLog(@"Latitude = %f", manager.location.coordinate.latitude);
        [_locationManager stopUpdatingLocation];
        
        CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
        [geoCoder reverseGeocodeLocation:manager.location completionHandler:^(NSArray *placemarks, NSError *error) {
            for (CLPlacemark * placemark in placemarks) {
                NSDictionary *test = [placemark addressDictionary];
                //  Country(国家)  State(城市)  SubLocality(区)
                NSLog(@"%@", [test objectForKey:@"Country"]);
                NSLog(@"%@", [test objectForKey:@"State"]);
                NSLog(@"%@", [test objectForKey:@"SubLocality"]);
                NSLog(@"%@", [test objectForKey:@"Street"]);
            }
        }];
    
    }

这样就很简单获取了当前位置的详细信息。

获得某一地点的经纬度

- (void)getLongitudeAndLatitudeWithCity:(NSString *)city
    {
        //city可以为中文
        NSString *oreillyAddress = city;
        CLGeocoder *myGeocoder = [[CLGeocoder alloc] init];
        [myGeocoder geocodeAddressString:oreillyAddress completionHandler:^(NSArray *placemarks, NSError *error) {
            if ([placemarks count] > 0 && error == nil)
            {
                NSLog(@"Found %lu placemark(s).", (unsigned long)[placemarks count]);
                CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0];
                NSLog(@"Longitude = %f", firstPlacemark.location.coordinate.longitude);
                NSLog(@"Latitude = %f", firstPlacemark.location.coordinate.latitude);
            }
            else if ([placemarks count] == 0 && error == nil)
            {
                NSLog(@"Found no placemarks.");
            }
            else if (error != nil)
            {
                NSLog(@"An error occurred = %@", error);
            }
        }];
    }

计算两点之间的距离

-
 (double)distanceByLongitude:(double)longitude1 
latitude:(double)latitude1 longitude:(double)longitude2 
latitude:(double)latitude2{
        CLLocation* curLocation = [[CLLocation alloc] initWithLatitude:latitude1 longitude:longitude1];
        CLLocation* otherLocation = [[CLLocation alloc] initWithLatitude:latitude2 longitude:longitude2];
        double distance  = [curLocation distanceFromLocation:otherLocation];//单位是m
        return distance;
    }


首先我们可以用上面的getLongitudeAndLatitudeWithCity方法获取某一个地点的经纬度。比如我们获取北京和上海的经纬度分别为:北京Longitude = 116.405285,Latitude = 39.904989 上海Longitude = 121.472644, Latitude = 31.231706, 那么北京和上海之间的距离就是:

double distance = [self distanceByLongitude:116.405285 latitude:39.904989 longitude:121.472644 latitude:31.231706];
    NSLog(@"Latitude = %f", distance);

计算的是大概的距离,可能没有那么精准。输入结果为:
distance = 1066449.749194

如果能帮到你我很高兴,有好东西也给我推荐一下~互粉哦~


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值