http://magicalboy.com/ios_gps_location/

 

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

步骤

    1. 加入 CoreLocation.framework , 导入头文件。
      #import <CoreLocation/CoreLocation.h>
    2. 在 AppDelegate.m 中加入检测是否启用位置服务功能。
      CLLocationManager *manager = [[CLLocationManager alloc] init];
      {
      ];
      ];
      ];
      }
      view rawgistfile1.mThis Gist brought to you by GitHub.

 

  1. 打开位置管理器。
    - (IBAction)openGPS:(id)sender {
    {
    ];
    ;
    // 越精确,越耗电!
    }
        
    // 开始定位
    }
    view rawgistfile1.mThis Gist brought to you by GitHub.
  2. 在 CLLocationManagerDelegate 中获取 GPS 位置信息。
    #pragma mark - CLLocationManagerDelegate
    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    {
    kCLErrorLocationUnknown:
    );
    ;
    kCLErrorDenied:
    );
    ;
    kCLErrorNetwork:
    );
    ;
    :
    );
    ;
    }
    }
     
    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {
    );
    );
    );
        
    );
    );
    );
        
    ];
    );
        
    // 取到精确GPS位置后停止更新
    {
    // 停止更新
    ];
    }
        
    ];
    ];
    }
    view rawgistfile1.mThis Gist brought to you by GitHub.

完整 Demo

GPS_iOS

相关文章: