In iOS development, the positioning and mapping feature is one of the more common features that you must develop based on 2 frameworks to join these 2 features.
(1) Corelocation: Used for geo-location, geo-coding, area monitoring, etc. (focus on function implementation).
(2) Mapkit: Used for map display, such as pin, route, overlay display, etc. (Focus on the interface display).
This article we will focus on the introduction of the next Corelocation framework
Corelocaiton Framework is the positioning of Baidu map is also in the Apple API based on the package.
One. iOS8.0 Pre-positioning configuration 1. Foreground location
2. Background positioning
Two. Positioning configuration after iOS8.0 1. Foreground location
2. Background positioning
@implementation Viewcontroller
Lazy Loading
-(Cllocationmanager *) locmanager{
if (!_locmanager) {
Create a location Manager
_locmanager = [[Cllocationmanager alloc] init];
Agent
_locmanager.delegate = self;
How many meters are positioned to move every
_locmanager.distancefilter = 3000;
Higher accuracy, more power consumption, longer positioning time
_locmanager.desiredaccuracy = kcllocationaccuracythreekilometers;
ios8.0+ positioning adaptation
if ([[Uidevice currentdevice].systemversion Floatvalue] >= 8.0) {
Front-end location authorization (by default, the location can not be obtained in the background, check the background mode of the site update, but there will be a blue bar)
[_locmanager requestwheninuseauthorization];
This method also works when the current authorization status is the foreground authorization
[_locmanager requestalwaysauthorization];
}
Allow background access to user location (iOS9.0)
if ([[Uidevice currentdevice].systemversion Floatvalue] >= 9.0)
{
Be sure to check the background mode location updates
_locmanager.allowsbackgroundlocationupdates = YES;
}
}
return _locmanager;
}
-(void) Viewdidload {
[Super Viewdidload];
Start updating user locations
[Self.locmanager startupdatinglocation];
}
#pragma mark-cllocationmanagerdelegate
/*
Called after update to location
@param Manager Location Manager
@param locations Position Array
*/
-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) Locations
{
NSLog (@ "Positioning to%@", locations);
Here locations stores the constant updated location coordinate values, takes the last value to the latest position, and if you do not want it to update the location continuously,
Cllocation *currentlocation = [locations Lastobject];
Get all current city names
Clgeocoder *geocoder = [[Clgeocoder alloc] init];
Reverse geo-compilation of address information based on latitude and longitude
[Geocoder reversegeocodelocation:currentlocation completionhandler:^ (Nsarray *array, Nserror *error)
{
if (Array.count > 0)
{
Clplacemark *placemark = [array objectatindex:0];
NSLog (@%@,placemark.name);//Specific location
Get the city
NSString *city = placemark.locality;
if (!city) {
The city information of the four municipalities can not be obtained by locality, but only by obtaining the province's method (if it is empty, it is known as the municipality)
City = Placemark.administrativearea;
}
Clplacemark *firstplacemark=[array Firstobject];
Warp latitude
Cllocationdegrees Latitude=firstplacemark.location.coordinate.latitude;
Cllocationdegrees Longitude=firstplacemark.location.coordinate.longitude;
Print Location City
NSLog (@ "Positioning complete:%@", city);
Print current latitude and longitude
NSLog (@ "%f,%f", latitude,longitude);
The system will keep updating the data until you choose to stop the update, because we just need to get the latitude and longitude, so get it and stop the update.
[Manager Stopupdatinglocation];
}else if (Error = = Nil && [array count] = = 0)
{
NSLog (@ "No results were returned.");
}else if (Error! = nil)
{
NSLog (@ "An error occurred =%@", error);
}
}];
Stop updating when you get a city location
[Manager Stopupdatinglocation];
}
/*
Called when the authorization state has changed
@param Manager Location Manager
@param status
*/
-(void) Locationmanager: (Cllocationmanager *) Manager didchangeauthorizationstatus: (clauthorizationstatus) status
{
Switch (status) {
The user has not yet decided
Case kclauthorizationstatusnotdetermined:
{
NSLog (@ "User has not decided");
Break
}
Ask restricted
Case kclauthorizationstatusrestricted:
{
NSLog (@ "Access restricted");
Break
}
When the location is closed and when the app is authorized as never, the call
Case Kclauthorizationstatusdenied:
{
Whether the positioning is available (whether to support positioning or whether the location is enabled)
if ([Cllocationmanager locationservicesenabled])
{
NSLog (@ "position is turned on, but rejected");
}else
{
NSLog (@ "location off, not available");
}
NSLog (@ "rejected");
Break
}
Get front and rear location authorization
Case Kclauthorizationstatusauthorizedalways:
Case kclauthorizationstatusauthorized://invalid, not recommended
{
NSLog (@ "Get front and rear location authorization");
Break
}
Get a front-office location authorization
Case Kclauthorizationstatusauthorizedwheninuse:
{
NSLog (@ "Get front desk location authorization");
Break
}
Default
Break
}
}
Failed to locate
-(void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror: (nserror *) error
{
NSLog (@ "Location failed%@", error);
}
@end