您的位置:首页 > 移动开发 > IOS开发

iOS8使用CoreLocation获取当前地理位置

2015-08-20 21:09 363 查看
1.导入CoreLocation.framework框架

#import <CoreLocation/CoreLocation.h>
2.定义CLLocationManager类型的属性

@property (strong, nonatomic) CLLocationManager *locationM;
3.初始化CLLocationManager, 开始定位
if ([CLLocationManager locationServicesEnabled]) {

<span style="white-space:pre"> </span>// 实例化一个位置管理器
self.locationM = [[CLLocationManager alloc] init];

_locationM.delegate = self;

// 设置定位精度
// kCLLocationAccuracyNearestTenMeters:精度10米
// kCLLocationAccuracyHundredMeters:精度100 米
// kCLLocationAccuracyKilometer:精度1000 米
// kCLLocationAccuracyThreeKilometers:精度3000米
// kCLLocationAccuracyBest:设备使用电池供电时候最高的精度
// kCLLocationAccuracyBestForNavigation:导航情况下最高精度,一般要有外接电源时才能使用
_locationM.desiredAccuracy = kCLLocationAccuracyBest;

// distanceFilter是距离过滤器,为了减少对定位装置的轮询次数,位置的改变不会每次都去通知委托,而是在移动了足够的距离时才通知委托程序
// 它的单位是米,这里设置为至少移动1000再通知委托处理更新;
_locationM.distanceFilter = kCLDistanceFilterNone;// 如果设为kCLDistanceFilterNone,则每秒更新一次;

[_locationM requestAlwaysAuthorization];
[_locationM requestWhenInUseAuthorization];
NSLog(@"999");
[_locationM startUpdatingLocation];

} else {
NSLog(@"请开启定位功能");
}4.实现CLLocationManagerDelegate代理方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//此处locations存储了持续更新的位置坐标值,取最后一个值为最新位置,如果不想让其持续更新位置,则在此方法中获取到一个值之后让locationManager stopUpdatingLocation
CLLocation *currentLocation = [locations lastObject];
CLLocationCoordinate2D coor = currentLocation.coordinate;
NSLog(@"经度:%f", coor.longitude);
NSLog(@"纬度:%f", coor.latitude);

//[self.locationM stopUpdatingLocation]; // 为了节省电量,可以在获取位置后暂时结束定位

}
5.获取位置失败的回调方法

<p class="p1"><span class="s1">- (</span><span class="s2">void</span><span class="s1">)locationManager:(</span><span class="s3">CLLocationManager</span><span class="s1"> *)manager didFailWithError:(</span><span class="s3">NSError</span><span class="s1"> *)error</span></p><p class="p1"><span class="s1">{</span></p><p class="p1"><span class="s1">    </span><span class="s4">/*</span></p><p class="p2"><span class="s1">     </span></p><p class="p3"><span class="s1">     kCLErrorLocationUnknown  = 0,         // location is currently unknown, but CL will keep trying</span></p><p class="p3"><span class="s1">     kCLErrorDenied,                       // Access to location or ranging has been denied by the user</span></p><p class="p3"><span class="s1">     kCLErrorNetwork,                      // general, network-related error</span></p><p class="p3"><span class="s1">     kCLErrorHeadingFailure,               // heading could not be determined</span></p><p class="p3"><span class="s1">     kCLErrorRegionMonitoringDenied,       // Location region monitoring has been denied by the user</span></p><p class="p3"><span class="s1">     kCLErrorRegionMonitoringFailure,      // A registered region cannot be monitored</span></p><p class="p3"><span class="s1">     kCLErrorRegionMonitoringSetupDelayed, // CL could not immediately initialize region monitoring</span></p><p class="p3"><span class="s1">     kCLErrorRegionMonitoringResponseDelayed, // While events for this fence will be delivered, delivery will not occur immediately</span></p><p class="p3"><span class="s1">     kCLErrorGeocodeFoundNoResult,         // A geocode request yielded no result</span></p><p class="p3"><span class="s1">     kCLErrorGeocodeFoundPartialResult,    // A geocode request yielded a partial result</span></p><p class="p3"><span class="s1">     kCLErrorGeocodeCanceled,              // A geocode request was cancelled</span></p><p class="p3"><span class="s1">     kCLErrorDeferredFailed,               // Deferred mode failed</span></p><p class="p3"><span class="s1">     kCLErrorDeferredNotUpdatingLocation,  // Deferred mode failed because location updates disabled or paused</span></p><p class="p3"><span class="s1">     kCLErrorDeferredAccuracyTooLow,       // Deferred mode not supported for the requested accuracy</span></p><p class="p3"><span class="s1">     kCLErrorDeferredDistanceFiltered,     // Deferred mode does not support distance filters</span></p><p class="p3"><span class="s1">     kCLErrorDeferredCanceled,             // Deferred mode request canceled a previous request</span></p><p class="p3"><span class="s1">     kCLErrorRangingUnavailable,           // Ranging cannot be performed</span></p><p class="p3"><span class="s1">     kCLErrorRangingFailure,               // General ranging failure</span></p><p class="p2"><span class="s1"></span>
</p><p class="p3"><span class="s1">     */</span></p><p class="p1"><span class="s1">    </span><span class="s3">NSLog</span><span class="s1">(</span><span class="s5">@"error = %@"</span><span class="s1">, error);</span></p><p class="p1"><span class="s1">}</span></p>

注意:
这时, 运行Xcode后并不会输出地理位置信息. 此时我们需要在Info.plist文件中添加下面两句:



部分内容转载自http://blog.devzeng.com/blog/ios8-corelocation-framework.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息