IOS8 previously used corelocation positioning
1, first define a global variable to record Cllocationmanager object, introduce corelocation.framework use #import <CoreLocation/CoreLocation.h>
1
@property (nonatomic, strong) Cllocationmanager *locationmanager;
2. Initialize the Cllocationmanager and start positioning
-(Cllocationmanager *) Locationmanager
{
#warning location service is not available, directly returns null
if (![ Cllocationmanager locationservicesenabled]) return nil;
if (!_locationmanager) {
Create locator
Self.locationmanager = [[Cllocationmanager alloc]init];
Set up Proxy
Self.locationManager.delegate = self;
}
return _locationmanager;
}
-(void) Viewdidload {
[Super Viewdidload];
Self.view.backgroundColor = [Uicolor Whitecolor];
Self.locationManager.desiredAccuracy = Kcllocationaccuracybest;
Self.locationManager.distanceFilter = 10;
[Self.locationmanager startupdatinglocation];
}
3, the implementation of Cllocationmanagerdelegate agent method
(1) Gets the location data, returns an array of cllocation, typically using one of the
-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) Locations
{
Cllocation *currlocation = [locations Lastobject];
NSLog (@ "Longitude =%f latitude =%f height =%f", CurrLocation.coordinate.latitude, CurrLocation.coordinate.longitude, Currlocation.altitude);
}
(2) A callback method to get the user's location data failed, which notifies the user
-(void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror: (nserror *) error
{
if ([error code] = = kclerrordenied)
{
Access is denied
}
if ([error code] = = Kclerrorlocationunknown) {
Unable to get location information
}
}
4. Close the position in Viewwilldisappear
-(void) Viewwilldisappear: (BOOL) animated
{
[Super viewwilldisappear:animated];
[_locationmanager stopupdatinglocation];
}
Using corelocation positioning in iOS8
1, before using corelocation need to call the following function "IOS8 dedicated":
IOS8 has made some modifications to the positioning, including the method of locating authorization, Cllocationmanager adds the following two methods:
(1) Always allow access to location information
-(void) requestalwaysauthorization;
(2) Allow access to location data during use of the application
-(void) requestwheninuseauthorization;
Examples are as follows:
-(void) Viewdidload {
[Super Viewdidload];
Self.view.backgroundColor = [Uicolor Whitecolor];
Self.locMgr.desiredAccuracy = Kcllocationaccuracybest;
Self.locMgr.distanceFilter = 10;
#warning Location Services, iOS8 add this sentence later
[Self.locmgr requestalwaysauthorization];
[Self.locmgr startupdatinglocation];
}
2. Add the following configuration in the Info.plist file:
(1) nslocationalwaysusagedescription
(2) Nslocationwheninuseusagedescription
IOS8 previously used with iOS8 corelocation positioning