Corelocation Import Framework: #import <CoreLocation/CoreLocation.h>
Basic properties and methods to understand:
Property:
- Location Manager: Cllocationmanager
- Request Location Permission: requestalwaysauthorization
- Start Get location: startupdatinglocation
- Stop Get location: stopupdatinglocation
- Authorized Authentication Status: Clauthorizationstatus
- Distance to filter positioning: Distancefilter
- Positioning required Accuracy: desiredaccuracy
- Information to navigate to: cllocation
- Create latitude and longitude points: cllocationcoordinate2d
Method:
- The authorization status has changed:
-(void) Locationmanager: (Cllocationmanager *) Manager didchangeauthorizationstatus: (clauthorizationstatus) status
- Get to location information:
-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) Locations
- Enter the listening area:
-(void) Locationmanager: (Cllocationmanager *) Manager didenterregion: (clregion *) region
- To leave the listening area:
-(void) Locationmanager: (Cllocationmanager *) Manager didexitregion: (clregion *) region
Basic operation of Corelocatio positioning:
After iOS8, the system will not default to help us call the location authorization, we need to proactively ask the user to grant us authorization, we need to call this method:
[Self.mgr requestalwaysauthorization];
And we also need to configure in the Info.plist file:
Nslocationwheninusedescription, allowing GPS descriptions to be obtained at the front desk
Nslocationalwaysusagedescription, allows the description of GPs to be acquired in the background
#import "ViewController.h"#import<CoreLocation/CoreLocation.h>@interfaceViewcontroller () <CLLocationManagerDelegate>/** * location Manager*/@property (nonatomic, strong) Cllocationmanager*Mgr;@end@implementationViewcontroller//Lazy Loading//Create Corelocation Manager-(Cllocationmanager *) mgr{if(!_mgr) {_mgr=[[Cllocationmanager alloc] init]; } return_mgr;}- (void) viewdidload {[Super viewdidload]; //set the location where the agent listener gets toSelf.mgr.Delegate=Self ; //Determine if the iOS8 if ([[[Uidevice currentdevice].systemversion Doublevalue] >= 8.0) {NSLog (@ "is iOS8"); Proactively require users to authorize our programs, the authorization status changes will notify the agent [self.mgr requestalwaysauthorization]; }Else { //Start listening (get location started)[Self.mgr startupdatinglocation]; } }//called when the authorization state has changed- (void) Locationmanager: (Cllocationmanager *) Manager didchangeauthorizationstatus: (clauthorizationstatus) status{if(Status = =kclauthorizationstatusnotdetermined) {NSLog (@"waiting for user authorization"); }Else if(Status = = Kclauthorizationstatusauthorizedalways | |Status==kclauthorizationstatusauthorizedwheninuse) {NSLog (@"Authorized Success"); //Start Positioning[Self.mgr startupdatinglocation]; }Else{NSLog (@"Authorization Failure"); }}#pragmaMark-cllocationmanagerdelegate//called when the location information is obtained (very high frequency of calls)- (void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations{//Positioning Information//cllocation *location = [locations lastobject]; }
iOS corelocation Location Services