Package development of Map location positioning

Source: Internet
Author: User

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
      */
      • (void) updateLocation;
    • (void) getLocation;

/**

    • Single-instance initialization method
      */
      • (Positiontool ) shareinfo;
        /*
    • Releasing a single case information
      */
      • (void) Freeinfo;

@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
      */
  • (cllocationcoordinate2d) Getcenter {
    return [_locationmanager location].coordinate;
    }
  • (void) UpdateLocation {
    [_locationmanager startupdatinglocation];
    }pragma Mark ========= Get location ==========
  • (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-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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.