zoukankan      html  css  js  c++  java
  • 定位CoreLocation

    //添加包:CoreLocation.framework
    //.plist文件:请求获得应用一直使用定位服务授权,注意使用此方法前要在info.plist中配置NSLocationAlwaysUsageDescription;请求获得应用使用时的定位服务授权,注意使用此方法前在要在info.plist中配置NSLocationWhenInUseUsageDescription,都设置为YES
    手动输入

    @property (nonatomic, retain) CLLocationManager *locationManager;
    //地理编码/逆地理编码
    @property (nonatomic, retain) CLGeocoder *geocoder;

    - (void)createLocationManager
    {
       
    self.locationManager = [[CLLocationManager alloc] init];
       
    self.geocoder = [[CLGeocoder alloc] init];
       
       
    if (![CLLocationManager locationServicesEnabled]) {
           
    NSLog(@"定位服务当前可能尚未打开,请设置打开");
           
    return ;
        }
       
    //如果没有授权则请求用户授权
       
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
            [
    self.locationManager requestWhenInUseAuthorization];
        }
    else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse) {
           
    //设置代理
           
    self.locationManager.delegate = self;
           
    //设置定位精度
           
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
           
    //定位频率,每隔多少米定位一次
    //        CLLocationDistance distance = 1000.0;//十米定位一次
           
    self.locationManager.distanceFilter = kCLDistanceFilterNone;
           
    //启动跟踪定位
            [
    self.locationManager startUpdatingLocation];
        }
     
    }

    #pragma 跟踪定位代理方法, 每次位置发生变化即执行
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
    {
       
    CLLocation *location=[locations firstObject];//取出第一个位置
       
    CLLocationCoordinate2D coordinate=location.coordinate;//位置坐标
        NSLog(@"经度:%f,纬度:%f,海拔:%f,航向:%f,行走速度:%f",coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed);
       
       [self getAddressByLatitude:coordinate.latitude longitude:coordinate.longitude];
        //如果不需要实时定位,使用完即使关闭定位服务
        [
    _locationManager stopUpdatingLocation];
    }
    //CLGeocoder类用于处理地理编码和逆地理编码(又叫反地理编码)功能.
    地理编码:根据给定的位置(通常是地名)确定地理坐标(经、纬度)。
    逆地理编码:可以根据地理坐标(经、纬度)确定位置信息(街道、门牌等
    #pragma mark 根据坐标取得地名(逆地理编码)
    -(void)getAddressByLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude{
       
    //反地理编码
       
    CLLocation *location=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
        [
    _geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
           
    CLPlacemark *placemark=[placemarks firstObject];
            NSLog(@"%@", placemark.name);//位置名
            NSLog(@"thoroughfare,%@",place.thoroughfare);       // 街道
            NSLog(@"subThoroughfare,%@",place.subThoroughfare); // 子街道
           
    NSLog(@"locality,%@",place.locality);               //
           
    NSLog(@"subLocality,%@",place.subLocality);         //
            NSLog(@"country,%@",place.country);                 // 国家
            //字典形式输出
    //        NSLog(@"详细信息:%@",placemark.addressDictionary);
        }];
    }
    //
  • 相关阅读:
    《原創》實現禁止 WTL CTabView 中標籤的拖曳行為。 (Disable Dragging Operation of CTabView)
    《轉貼》ATL NTService 運作流程
    《轉貼》WTL 之 m_hWndMDIClient
    《原創》加強版的 C++ 字串型別
    《原創》建立最基礎的 Irrlicht 3D 引擎的應用框架。(for vc2008)
    《轉貼》關於 ios::app 與 ios::ate 簡易說明
    《轉貼》WTL for MFC programmer 系列文章
    《轉貼》以Virtual Inheritance及Template讓C++能實現C#中的sealed
    Oracle学习笔记(concat和substr)
    Oralce学习笔记(sql取一张表的数据插入另一张表)
  • 原文地址:https://www.cnblogs.com/yuhaojishuboke/p/5155859.html
Copyright © 2011-2022 走看看