In many app development often use the positioning function, for this common method is necessary for its encapsulation use
Not much to say directly on the code: the use of a single-case design mode:
`
Import <foundation/foundation.h>import <MapKit/MapKit.h>
@protocol positiontooldelegate <NSObject>
@optional
/**
- Notification value when position changes
*/
- (void) noticepositionchanged;
@end
@interface positiontool:nsobject<cllocationmanagerdelegate,positiontooldelegate> {
Cllocationmanager _locationmanager;
Nsmutablearray _locationarr;
}
@property (nonatomic,assign) cllocationdegrees latitude;//Longitude
@property (nonatomic,assign) cllocationdegrees longtitude;//latitude
/**
- Location Area Information
/
@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;
/**
- Adding callback proxy methods
*/
- (void) AddDelegate: (id) delegate;
/**
- Remove Callback Proxy method
*/
- (void) Removedelegate: (id) delegate;
/**
- Get Location Center Information
*/
- (cllocationcoordinate2d) Getcenter;
/**
- Update location
*/
/**
- Single-instance initialization method
*/
- (Positiontool ) shareinfo;
/*
- Releasing a single case information
*/
@end
.m方法的实现
- (positiontool*) Shareinfo {
@synchronized (self) { if (_positionTool == nil) { _positionTool=[[PositionTool alloc] init]; [email protected]"没有定位"; }
}
return _positiontool;
}
/**
- Releasing a singleton implementation
*/
(void) Freeinfo {
if (_positiontool) {
nil;
}
}
(ID) Init {
Self=[super Init];
if (self) {
_locationArr=[[NSMutableArray alloc] init];
}
return self;
}
/**
- Proxy callback Add and remove implementation methods
*
- @param delegate Agent
*/
- (void) AddDelegate: (id<positiontooldelegate>) Delegate {
[_locationarr Addobject:delegate];
}
- (void) Removedelegate: (id) Delegate {
[_locationarr Removeobject:delegate];
}
/**
- Get location information
*/
pragma mark-Call Map Proxy method
- (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;
}
}
Get latitude and longitude
(void) Locationmanager: (Cllocationmanager ) Manager didupdatelocations: (nsarray<cllocation > ) Locations {
Cllocation Newlocation=locations[0];
Cllocationcoordinate2d oldcoordinate=newlocation.coordinate;
NSLog (@ "Old Longitude:%f, old Latitude:%f", oldcoordinate.longitude,oldcoordinate.latitude);
NSLog (@ "New Longitude:%f, new Latitude:%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 *placeIn 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");
}
`
Use time to add directly to the place where you use it
[[Positiontool Shareinfo] getLocation];
Wen/mikko (author of Jane's book)
Original link: Http://www.jianshu.com/p/39228fd5da68
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".
Package development of Map location positioning