zoukankan      html  css  js  c++  java
  • ios---CoreLocation框架实现定位功能

    CoreLocation框架实现定位功能(iOS8.0之后)

    //
    //  ViewController.m
    //  定位
    //
    //  Created by admin on 2017/9/20.
    //  Copyright © 2017年 admin. All rights reserved.
    //
    
    #import "ViewController.h"
    #import <CoreLocation/CoreLocation.h>
    
    @interface ViewController ()<CLLocationManagerDelegate>
    @property(nonatomic,strong)CLLocationManager *locationManager;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self startLocation];
    }
    -(void)startLocation
    {
        //判断用户是否打开了定位功能
        if([CLLocationManager locationServicesEnabled]){
            if(!_locationManager){
                _locationManager=[[CLLocationManager alloc]init];
                //设置代理
                [self.locationManager setDelegate:self];
                //设置定位精确度,精确度越高,越耗电
                [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
                //设置多远距离定位一次
                [self.locationManager setDistanceFilter:100];
                //开始获取授权,打开定位
                [self.locationManager requestWhenInUseAuthorization];
                //开始定位
                [self.locationManager startUpdatingLocation];
            }else{
                [self.locationManager startUpdatingLocation];
            }
        }else{
            NSLog(@"%d",666);
        }
    }
    
    #pragma mark -CLLocationManagerDelegate
    //代理方法监听定位服务状态的变化
    -(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
        switch (status) {
            case kCLAuthorizationStatusNotDetermined:
                if([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]){
                    [_locationManager requestAlwaysAuthorization];
                    NSLog(@"用户还未决定授权");
                }
                break;
            case kCLAuthorizationStatusAuthorizedWhenInUse:
                if([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]){
                    [_locationManager requestAlwaysAuthorization];
                    NSLog(@"定位服务授权状态被允许在使用应用程序的时候");
                }
                break;
            case kCLAuthorizationStatusRestricted:
            {
                NSLog(@"访问受限");
                break;
            }
            case kCLAuthorizationStatusAuthorizedAlways:
                if([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]){
                    [_locationManager requestAlwaysAuthorization];
                    NSLog(@"定位服务授权状态已经被用户允许在任何状态下获取位置信息。包括监测区域、访问区域、或者在有显著的位置变化的时候");
                }
                break;
            case kCLAuthorizationStatusDenied:
                NSLog(@"被拒绝了");
                break;
            default:
                break;
        }
    }
    //代理方法返回locationd 信息
    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
        NSLog(@"%@",locations);
        CLLocation *currLocation=locations.lastObject;
        NSTimeInterval locatinAge=-[currLocation.timestamp timeIntervalSinceNow];
        NSLog(@"%f----%f",[currLocation.timestamp timeIntervalSince1970],locatinAge);
        //关闭定位
        [self.locationManager stopUpdatingLocation];
        CLLocation *location=locations.lastObject;
        [self reverseGeocoder:location];
    }
    //地理反编码
    -(void)reverseGeocoder:(CLLocation *)currentLocation{
        CLGeocoder *geocoder=[[CLGeocoder alloc]init];
        [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            if(error || placemarks.count==0){
                NSLog(@"反编码失败");
            }else{
                CLPlacemark *placemark=placemarks.firstObject;
                NSLog(@"placemark:%@",[placemark addressDictionary]);
                NSString *city=[[placemark addressDictionary]objectForKey:@"City"];
                NSLog(@"%@",city);
            }
        }];
    }
    
    @end
    
    
    github:https://github.com/Frankltf/ios-CoreLocation/tree/features-one
  • 相关阅读:
    python和搜索
    Flask---ajax(jquery)交互
    Flask--修改默认的static文件夹的方法
    Flask设计带认证token的RESTful API接口[翻译]
    Python 和 Flask实现RESTful services
    等差数列偶数被除2删除后的恢复问题(2018小马智行秋招计算机视觉第三道编程题)
    Leetcode 140 单词拆分II: 字符串s在字典wordDict中有多少种拆分方法。
    LeetCode 139 单词拆分:字符串s能否分割为字符串数组words(wordDict)中字符串的组合?(某未来公司面试题目)
    ROS时间概念总结:ros::Time、ros::Duration、定时器ros::Timer&ros::Rate
    ++i、i++、i+=1、i=i+1的区别
  • 原文地址:https://www.cnblogs.com/frankltf/p/7560867.html
Copyright © 2011-2022 走看看