• LocationDemo定位


      1 #import "ViewController.h"
      2 #import <CoreLocation/CoreLocation.h>
      3 @interface ViewController ()<CLLocationManagerDelegate>
      4 
      5 @property (nonatomic,strong)CLLocationManager *manager ;
      6 @end
      7 
      8 @implementation ViewController
      9 
     10 - (void)viewDidLoad {
     11     [super viewDidLoad];
     12     
     13     /*
     14      定位的方式
     15      1.手机基站:(依赖于手机站(信息塔)的密集程度)
     16      2.GPS卫星定位:精确度高,但是费电
     17      3.WIFI:上网的IP地址,(必须要能上网)
     18      */
     19     
     20     //定位管理器
     21     self.manager = [[CLLocationManager alloc] init];
     22     
     23 //    if (![CLLocationManager locationServicesEnabled]) {
     24 //        
     25 //        NSLog(@"定位服务当前可能尚未打开,请设置打开!");
     26 //        
     27 //        return ;
     28 //    }
     29     
     30     
     31     
     32     
     33     
     34     //判断当前的系统版本号
     35     if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
     36         
     37         //说明当前的系统是大于或者等于8.0的系统版本号
     38         
     39         // 需要在plist文件中添加默认缺省的字段“NSLocationAlwaysUsageDescription”,这个提示是:“允许应用程序在您并未使用该应用程序时访问您的位置吗?”NSLocationAlwaysUsageDescription对应的值是告诉用户使用定位的目的或者是标记。
     40         
     41         //需要在plist文件中添加默认缺省的字段“NSLocationWhenInUsageDescription”,这个时候的提示是:“允许应用程序在您使用该应用程序时访问您的位置吗?”
     42         
     43         //向用户请求我们的程序使用位置
     44         [self.manager requestAlwaysAuthorization];
     45         [self.manager requestWhenInUseAuthorization];
     46         
     47     }
     48     
     49     //设置定位精度(更新距离)---实质就是让代理方法重新走一次
     50     self.manager.distanceFilter = 10.0 ;
     51     
     52     //设置代理
     53     _manager.delegate = self ;
     54     
     55     //启动实时定位的功能
     56     [_manager startUpdatingLocation];
     57     
     58     //启动手机朝向的功能
     59 }
     60 
     61 #pragma mark --更新最新的位置
     62 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
     63 {
     64     
     65 //    NSLog(@"%@",locations);
     66     
     67     //获取当前的最新位置
     68     CLLocation *location = (CLLocation *)[locations lastObject];
     69     
     70     //从位置信息中获取经纬度
     71     double lat = location.coordinate.latitude ;
     72     double lon = location.coordinate.longitude ;
     73     
     74     NSLog(@"%f   %f",lat,lon);
     75     
     76     
     77     //使用位置反编码解析位置信息
     78     CLGeocoder *geocoder = [[CLGeocoder alloc] init];
     79     
     80     //使用该对象获得该位置信息
     81     
     82     [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
     83     
     84         //遍历解析出来的位置信息
     85         for (CLPlacemark *placeMark in placemarks) {
     86             
     87             NSLog(@"%@",placeMark);
     88             
     89             /*
     90             NSString *name=placemark.name;  //地名
     91             NSString *thoroughfare=placemark.thoroughfare;  //街道
     92             NSString *subThoroughfare=placemark.subThoroughfare; //街道相关信息,例如门牌等
     93             NSString *locality=placemark.locality; // 城市
     94             NSString *subLocality=placemark.subLocality; // 城市相关信息,例如标志性建筑
     95             NSString *administrativeArea=placemark.administrativeArea; // 州
     96             NSString *subAdministrativeArea=placemark.subAdministrativeArea; //其他行政区域信息
     97             NSString *postalCode=placemark.postalCode; //邮编
     98             NSString *ISOcountryCode=placemark.ISOcountryCode; //国家编码
     99             NSString *country=placemark.country; //国家
    100             NSString *inlandWater=placemark.inlandWater; //水源、湖泊
    101             NSString *ocean=placemark.ocean; // 海洋
    102             NSArray *areasOfInterest=placemark.areasOfInterest; //关联的或利益相关的地标
    103             
    104             */
    105         }
    106         
    107     }];
    108     
    109     
    110     //如果不需要实时定位,使用完即使关闭定位服务
    111     [_manager stopUpdatingLocation];
    112     
    113 }
    114 
    115 
    116 #pragma amrk --更新手机朝向
    117 -(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
    118 {
    119     //获得手机里面的磁场方面
    120     int header = newHeading.magneticHeading ;
    121     
    122     if ((315<=header && header<=360) || (0<header && header<=45)){
    123         
    124         NSLog(@"朝向北面");
    125     }
    126     else if (45< header && header<=135){
    127         
    128         NSLog(@"朝向东面");
    129     }
    130     else if (135< header && header<=225){
    131         
    132         NSLog(@"朝向南面");
    133     }
    134     else if (225< header && header<315){
    135         
    136         NSLog(@"朝向西面");
    137     }
    138         
    139 }
  • 相关阅读:
    设置为自动启动的WindowService没有开机启动
    Asp.Net部署问题
    MSDTC的折磨
    C# WinForm 边框阴影窗体
    升级DotNetNuke
    常用缩写
    DotNetNuke的升级路径
    日本語文法勉強
    PostSubmitter~在WEB应用程序以外的其他程序里提交Web请求的类
    vue中的锚链接跳转问题
  • 原文地址:https://www.cnblogs.com/yyxblogs/p/4872870.html
Copyright © 2020-2023  润新知