zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    dijkstra算法模板 -- 最短路
    0-1背包
    POJ 1456-Supermarket(贪心+并查集)
    CodeForces 556C
    CodeForces
    POJ 2253-Frogger(Floyd变形)
    POJ 1251-Jungle Roads(最小生成树)
    HDU 1846-Brave Game(巴什博弈)
    HDU 1233-还是畅通工程(经典最小生成树)
    51Nod 1649-齐头并进(最短路dijkstra)
  • 原文地址:https://www.cnblogs.com/youmomo/p/4088509.html
Copyright © 2011-2022 走看看