在很多的APP开发中经常使用到定位功能,对于这种常用的方法很有必要对其封装使用
话不多说直接上代码:使用到了单例设计模式:
`
import <Foundation/Foundation.h>
import <MapKit/MapKit.h>
@protocol PositionToolDelegate <NSObject>
@optional
/**
- 位置改变时通知传值
*/- (void)noticePositionChanged;
@end
@interface PositionTool : NSObject<CLLocationManagerDelegate,PositionToolDelegate> {
CLLocationManager _locationManager;
NSMutableArray _locationArr ;
}
@property (nonatomic,assign) CLLocationDegrees latitude;//经度
@property (nonatomic,assign) CLLocationDegrees longtitude;//纬度
/**
- 位置地区信息
/
@property (nonatomic,strong) NSString positionName;
@property (nonatomic,strong) NSString province;
@property (nonatomic,strong) NSString city;
@property (nonatomic,strong) NSString *area;
//@property (nonatomic,strong) id<PositionToolDelegate> delegate;
/**
- 添加回调代理方法
*/- (void)addDelegate:(id)delegate;
/**
- (void)addDelegate:(id)delegate;
- 移除回调代理方法
*/- (void)removeDelegate:(id)delegate;
/**
- (void)removeDelegate:(id)delegate;
- 获取位置中心信息
*/- (CLLocationCoordinate2D)getCenter;
/**
- (CLLocationCoordinate2D)getCenter;
- 更新位置
*/- (void)updateLocation;
- (void)getLocation;
/**
- 单例初始化方法
*/- (PositionTool )shareInfo;
/*
- (PositionTool )shareInfo;
- 释放单例信息
*/- (void)freeInfo;
@end.m方法的实现
- (PositionTool*)shareInfo {
@synchronized(self) {if (_positionTool == nil) { _positionTool=[[PositionTool alloc] init]; [email protected]"没有定位"; }
}
return _positionTool;
}
/**- 释放单例实现
*/
- 释放单例实现
- (void)freeInfo {
if (_positionTool) {_positionTool = nil;
}
} - (id)init {
self=[super init];
if (self) {_locationArr=[[NSMutableArray alloc] init];
}
return self;
}
/**- 代理回调添加和删除实现方法
* - @param delegate 代理
*/
- 代理回调添加和删除实现方法
- (void)addDelegate:(id<PositionToolDelegate>)delegate {
[_locationArr addObject:delegate];
} - (void)removeDelegate:(id)delegate {
[_locationArr removeObject:delegate];
}
/**
- 获取位置信息
*/
- (CLLocationCoordinate2D)getCenter {
return [_locationManager location].coordinate;
} - (void)updateLocation {
[_locationManager startUpdatingLocation];
}
pragma mark ========= 获取位置 ==========
- (void)getLocation {
if ([CLLocationManager locationServicesEnabled]) {_locationManager=[[CLLocationManager alloc] init]; _locationManager.delegate=self; //精度最高,耗电最大 _locationManager.desiredAccuracy=kCLLocationAccuracyBest; _locationManager.distanceFilter = 200; [_locationManager startUpdatingLocation]; if ([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0) { [_locationManager requestAlwaysAuthorization]; }
}
}
pragma mark - 调用地图代理方法
- (void)locationManager:(CLLocationManager )manager didFailWithError:(NSError )error {
NSLog(@"%@",error);
} -
(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
switch (status) {case kCLAuthorizationStatusNotDetermined: if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { [_locationManager requestAlwaysAuthorization]; } break; default: break;
}
}
//获取经纬度 - (void)locationManager:(CLLocationManager )manager didUpdateLocations:(NSArray<CLLocation > )locations {
CLLocation newLocation=locations[0];
//CLLocationCoordinate2D oldCoordinate=newLocation.coordinate;
//NSLog(@"旧的经度:%f,旧的纬度:%f",oldCoordinate.longitude,oldCoordinate.latitude);
//NSLog(@"新的经度:%f,新的纬度:%f",newLocation.coordinate.longitude,newLocation.coordinate.latitude);
self.latitude=newLocation.coordinate.latitude;
self.longtitude=newLocation.coordinate.longitude;
[manager stopUpdatingLocation];CLGeocoder geocoder=[[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark > _Nullable placemarks, NSError _Nullable error) {for (CLPlacemark *place in placemarks) { self.positionName=place.name; self.city=place.locality; self.area=place.subLocality; NSLog(@"%@",self.area); } for (id<PositionToolDelegate>delegate in _locationArr) { if (delegate && [delegate respondsToSelector:@selector(noticePositionChanged)]) { [delegate noticePositionChanged]; } }
}];
for (id<PositionToolDelegate> delegate in _locationArr) {
if (delegate && [delegate respondsToSelector:@selector(noticePositionChanged)]) { [delegate noticePositionChanged]; }
}
}
-(void)locationManager:(CLLocationManager )manager didUpdateToLocation:(CLLocation )newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"aaa%@", @"ok");
}
`
使用时候直接在使用到的地方加入
[[PositionTool shareInfo] getLocation];
文/Mikko(简书作者)
原文链接:http://www.jianshu.com/p/39228fd5da68
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。