http://magicalboy.com/ios_gps_location/
在 iOS 平台上使用 CLLocationManager 获取 GPS 位置信息,比如经度,纬度,海拔高度等是很简单的事情。
步骤
- 加入 CoreLocation.framework , 导入头文件。
#import <CoreLocation/CoreLocation.h>
- 在 AppDelegate.m 中加入检测是否启用位置服务功能。CLLocationManager *manager = [[CLLocationManager alloc] init];{];];];}
- 打开位置管理器。- (IBAction)openGPS:(id)sender {{];;// 越精确,越耗电!}// 开始定位}
- 在 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位置后停止更新{// 停止更新];}];];}