XMPP-利用CoreLocation获取地理位置以及CLLocationManager 为什么就不调用代理

最近做基于XMPP的即时通讯,把相关内容总结整理下来看看!

一.利用CoreLocation获取地理位置

利用CoreLocation,必须在frameworks里面加入"CoreLocation.framework",然后类中import <CoreLocation/CoreLocation.h>

1.定义成员变量

#import "LocationHelper.h"
@interface LocationHelper ()<CLLocationManagerDelegate>
{
    CLLocationManager *_locationManager;
}
@property(nonatomic,copy)GetLocationCompledBlock getLocationCompledBlock;

2.实现单例

#pragma mark - shareLocationHelper单例

+ (id)shareLocationHelper{
    static LocationHelper *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[LocationHelper alloc] init];
    });
    return instance;
}

3.进行初始化CLLocationManager位置管理器

- (void)setup {
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.delegate = self;
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    _locationManager.distanceFilter = 5.0;
    [_locationManager requestAlwaysAuthorization];
}

desiredAccuracy为设置定位的精度,可以设为最优,装置会自动用最精确的方式去定位。

distanceFilter是距离过滤器,为了减少对定位装置的轮询次数,位置的改变不会每次都去通知委托,而是在移动了足够的距离时才通知委托程序,它的单位是米,这里设置为移动5再通知委托处理更新

4.实现外界要求获取位置的请求

- (void)getCurrentGeolocationsCompled:(GetLocationCompledBlock)compled{
    self.getLocationCompledBlock = compled;
    [_locationManager startUpdatingLocation];
}

在此使用startUpdatingLocation启动定位管理了,一般来说,在不需要更新定位时最好关闭它,用stopUpdatingLocation,可以节省电量。

5.实现CLLocationManagerDelegate代理方法获取位置更新信息

#pragma mark - CLLocationManager Delegate

// 代理方法实现
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    CLGeocoder* geocoder = [[CLGeocoder alloc] init];
    
    [geocoder reverseGeocodeLocation:newLocation completionHandler:
     ^(NSArray* placemarks, NSError* error) {
         if (self.getLocationCompledBlock) {
             self.getLocationCompledBlock(placemarks);
         }
     }];
    [manager stopUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    [manager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    switch (status) {
        casekCLAuthorizationStatusNotDetermined:
            if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
                [_locationManager requestAlwaysAuthorization];
            }
            break;
        default:
            break;
    } 
}

6.控制器中调用方法获取位置信息后加TAG发送

[self.locationHelper getCurrentGeolocationsCompled:^(NSArray *placemarksA) {
        CLPlacemark *placemark = [placemarksA lastObject];
        if (placemark) {
            NSDictionary *addressDictionary = placemark.addressDictionary;
            NSArray *formattedAddressLines = [addressDictionary valueForKey:@"FormattedAddressLines"];
            NSString *geoLocations = [formattedAddressLines lastObject];
            if (geoLocations) {
                //geoLocations===中国****
                [weakSelf didSendGeolocationsMessageWithGeolocaltions:geoLocations];
            }
        }
}];

 

二.CLLocationManager为什么就不调用代理

代码方法开开心心的写完了,但是在发送地理位置消息的时候老是没有消息,郁闷了,断点发现代理根本都没进,最后分析找资料得到解决

上述编码均完成后在Plist文件中添加字段NSLocationAlwaysUsageDescription

 

posted on 2014-11-10 23:31  悠漠漠  阅读(537)  评论(0编辑  收藏  举报