封装CLLocationManager定位获取经纬度


创建调用方法,在.h文件中

#import <Foundation/Foundation.h>

@interface RMMapLocation : NSObject
{
    void (^saveGpsCallBack)(double lattitude,double longitude);
}
+ (void)getGps:(void(^)(double lattitude,double longitude))block;
+ (void)stop;

在.m文件中进行方法的实现

#import "RMMapLocation.h"

@interface RMMapLocation ()<CLLocationManagerDelegate>
@property (strong, nonatomic)CLLocationManager *locManager;

@end

@implementation RMMapLocation

+ (instancetype)sharedGpsManager
{
    static id mapLocation;    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!mapLocation) {
            mapLocation = [[RMMapLocation alloc] init];
        }
    });
    return mapLocation;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self getCurrentLocation];
    }
    return self;
}

- (void)getCurrentLocation
{
    self.locManager = [[CLLocationManager alloc] init];
    self.locManager.delegate = self;
    self.locManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locManager.distanceFilter = 10.0;
}

- (void)getGps:(void (^)(double lat, double lng))gps
{
    if ([CLLocationManager locationServicesEnabled] == FALSE) {
        return;
    }
    saveGpsCallBack = [gps copy];
    [self.locManager startUpdatingLocation];
}

+ (void)getGps:(void (^)(double, double))block
{
    [[RMMapLocation sharedGpsManager] getGps:block];
}

- (void)stop
{
    [self.locManager stopUpdatingLocation];
}

+ (void)stop
{
    [[RMMapLocation sharedGpsManager] stop];
}

#pragma mark - locationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    RMLog(@"%f,%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude);
    double latitude = newLocation.coordinate.latitude;
    double longitude = newLocation.coordinate.longitude;
    if (saveGpsCallBack) {
        saveGpsCallBack(latitude,longitude);
    }
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    RMLog(@"%@",error);
//    [RMUtils showMessage:@"定位失败"];
}

在需要调用的文件中引入头文件后调用方法的实现(如需只定位一次,则调用stop方法即可)

[RMMapLocation getGps:^(double lattitude, double longitude) {
        RMLog(@"%f---%f",lattitude,longitude);
}];


demo地址:http://download.csdn.net/detail/sinat_28585351/9491130

 https://github.com/Raymon-lau/CLLocationManager





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值