CLLocationManager 获取 GPS 位置信息

 http://magicalboy.com/ios_gps_location/

 

在 iOS 平台上使用 CLLocationManager 获取 GPS 位置信息,比如经度,纬度,海拔高度等是很简单的事情。

步骤

    1. 加入 CoreLocation.framework , 导入头文件。
      #import <CoreLocation/CoreLocation.h>
    2. 在 AppDelegate.m 中加入检测是否启用位置服务功能。
      CLLocationManager * manager = [[ CLLocationManager alloc ] init ];
           if ( manager . locationServicesEnabled == NO ) {
               UIAlertView * servicesDisabledAlert = [[ UIAlertView alloc ] initWithTitle: @"Location Services Disabled" message: @"You currently have all location services for this device disabled. If you proceed, you will be asked to confirm whether location services should be reenabled." delegate: nil cancelButtonTitle: @"OK" otherButtonTitles: nil ];
               [ servicesDisabledAlert show ];
               [ servicesDisabledAlert release ];
           }
      view raw gistfile1.m This Gist brought to you by  GitHub.

 

  1. 打开位置管理器。
    - ( IBAction ) openGPS: ( id ) sender {
         if ( locationManager == nil ) {
             locationManager = [[ CLLocationManager alloc ] init ];
             locationManager . delegate = self ;
             locationManager . desiredAccuracy = kCLLocationAccuracyBest ; // 越精确,越耗电!
         }
        
         [ locationManager startUpdatingLocation ]; // 开始定位
    }
    view raw gistfile1.m This Gist brought to you by  GitHub.
  2. 在 CLLocationManagerDelegate 中获取 GPS 位置信息。
    #pragma mark - CLLocationManagerDelegate
    - ( void ) locationManager: ( CLLocationManager * ) manager didFailWithError: ( NSError * ) error {
         switch ( error . code ) {
             case kCLErrorLocationUnknown:
                 NSLog ( @"The location manager was unable to obtain a location value right now." );
                 break ;
             case kCLErrorDenied:
                 NSLog ( @"Access to the location service was denied by the user." );
                 break ;
             case kCLErrorNetwork:
                 NSLog ( @"The network was unavailable or a network error occurred." );
                 break ;
             default :
                 NSLog ( @"未定义错误" );
                 break ;
         }
    }
     
    - ( void ) locationManager: ( CLLocationManager * ) manager didUpdateToLocation: ( CLLocation * ) newLocation fromLocation: ( CLLocation * ) oldLocation
    {
         NSLog ( @"oldLocation.coordinate.timestamp:%@" , oldLocation . timestamp );
         NSLog ( @"oldLocation.coordinate.longitude:%f" , oldLocation . coordinate . longitude );
         NSLog ( @"oldLocation.coordinate.latitude:%f" , oldLocation . coordinate . latitude );
        
         NSLog ( @"newLocation.coordinate.timestamp:%@" , newLocation . timestamp );
         NSLog ( @"newLocation.coordinate.longitude:%f" , newLocation . coordinate . longitude );
         NSLog ( @"newLocation.coordinate.latitude:%f" , newLocation . coordinate . latitude );
        
         NSTimeInterval interval = [ newLocation . timestamp timeIntervalSinceDate: oldLocation . timestamp ];
         NSLog ( @"%lf" , interval );
        
         // 取到精确GPS位置后停止更新
         if ( interval < 3 ) {
             // 停止更新
             [ locationManager stopUpdatingLocation ];
         }
        
         latitudeLabel . text = [ NSString stringWithFormat: @"%f" , newLocation . coordinate . latitude ];
         longitudeLabel . text = [ NSString stringWithFormat: @"%f" , newLocation . coordinate . longitude ];
    }
    view raw gistfile1.m This Gist brought to you by  GitHub.

完整 Demo

GPS_iOS

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值