码迷,mamicode.com
首页 > 移动开发 > 详细

如何使iOS获取的地理位置有效

时间:2014-10-21 11:35:00      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:des   blog   io   os   ar   strong   sp   div   on   

关于CLLocationManager获取地理位置遇到的一些小事;

项目使arc  

第一步:获取是否允许定位

-(BOOL)IsLocationServicesEnabled
{
    /**
     * [CLLocationManager locationServicesEnabled] 系统设置允许定位服务是否开启
     * [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied  APP是否开启定位服务
     */
    return ([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied);
}

第二步:开始定位,创建全局变量 (个人理解:如果CLLocationManager的属性不是全局的在ARC项目中,出了函数体 局部变量就会被release)

@property (nonatomic, strong) CLLocationManager *userLocation;

  

-(void)getUserLocationInfomation
{
    if ([self IsLocationServicesEnabled])
    {
        
        if (!self.userLocation)
        {
            self.userLocation = [[CLLocationManager alloc]init];
        }
        
        self.userLocation.delegate = self;
        //选择定位的方式为最优的状态
        self.userLocation.desiredAccuracy = kCLLocationAccuracyBest;
        //发生事件的最小距离间隔
        self.userLocation.distanceFilter = kCLDistanceFilterNone;
        [self.userLocation startUpdatingLocation];
    }
}

第三部分:delegate回调

#pragma locationManager delegate

-(void)locationManager:(CLLocationManager *)manager
   didUpdateToLocation:(CLLocation *)newLocation
          fromLocation:(CLLocation *)oldLocation
{
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
    if (locationAge > 180.0) return;
    // 测试该水平精度是否有效
    if (newLocation.horizontalAccuracy < 0) return;
    // 写你的代码 现在拿到的newLocation是有效的
[self.userLocation stopUpdatingLocation];
self.userLocation.delegate = nil;

}

-(void)locationManager:(CLLocationManager *)manager
      didFailWithError:(NSError *)error
{
    
}

  

如何使iOS获取的地理位置有效

标签:des   blog   io   os   ar   strong   sp   div   on   

原文地址:http://www.cnblogs.com/swiftAngel/p/4039596.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!