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

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


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值