First, we need to import the Corelocation system framework into the project. The header file is then introduced into our controller.
#import <CoreLocation/CoreLocation.h>
Then, a Cllocationmanager object is declared as a member variable for locating the latitude and longitude coordinates and complying with the Protocol Cllocationmanager protocol.
@interface Tyviewcontroller () <CLLocationManagerDelegate> {
Cllocationmanager *_locationmanager;
}
Implement the Proxy method.
-(void) Locationmanager: (Cllocationmanager *) Manager didupdatetolocation: (cllocation *) newlocation fromlocation: ( Cllocation *) oldlocation
{
Show longitude to label
Self.longitude.text = [NSString stringwithformat:@ "%lf", NewLocation.coordinate.longitude];
The latitude reality to the label
Self.latitude.text = [NSString stringwithformat:@ "%lf", NewLocation.coordinate.latitude];
Get the name of the city you are currently in
Clgeocoder *geocoder = [[Clgeocoder alloc] init];
To compile the address information according to the latitude and longitude of the reverse geography
[Geocoder reversegeocodelocation:newlocation completionhandler:^ (Nsarray *array, Nserror *error) {
if (Array.count > 0) {
Clplacemark *placemark = [array objectatindex:0];
Display all the information you get to the label
Self.location.text = Placemark.name;
Get the city
NSString *city = placemark.locality;
if (!city) {
The urban information of the four municipalities cannot be obtained through locality, only by acquiring the provincial method (if the city is empty, it is known as the municipality).
City = Placemark.administrativearea;
}
NSLog (@ "city =%@", city);
_citylable.text = City;
[_citybutton settitle:city Forstate:uicontrolstatenormal];
}
else if (Error = = Nil && [array count] = 0)
{
NSLog (@ "No results were returned.");
}
else if (Error!= nil)
{
NSLog (@ "An error occurred =%@", error);
}
}];
The system keeps updating the data until it chooses to stop the update, because we only need to get a latitude and longitude, so we stop the update after we get it.
[Manager Stopupdatinglocation];
}
Finally, the locator manager is initialized in Viewdidload.
-(void) viewdidload
{
[Super Viewdidload];
[Self initializelocationservice];
Do no additional setup after loading the view.
}
-(void) Initializelocationservice {
Initializing the location Manager
_locationmanager = [[Cllocationmanager alloc] init];
Set up agents
_locationmanager.delegate = self;
Set positioning accuracy to M
_locationmanager.desiredaccuracy = Kcllocationaccuracybest;
Set Filter to None
_locationmanager.distancefilter = Kcldistancefilternone;
Start positioning
[_locationmanager startupdatinglocation];
}
Next compile and run the program, manually configure the location coordinates on the iOS simulator, you can print out the latitude and longitude coordinates corresponding to the city name (true machine can directly print out the current city name).
The Clplacemark class encapsulates a number of geographic information attributes, including complete addresses (large to national, small to street) and geographical names, etc., which can be used as appropriate.
PS: If the location of the street information for English, then please deal with localization, while the simulator to adjust the language for Chinese.
The following are supplementary to IOS8:
If normal positioning is required, the IOS8 needs to handle two additional locations relative to iOS7.
1. Add two fields to the plist file of the project: the Nslocationalwaysusagedescription,nslocationwheninuseusagedescription,type type is string, Value can be filled according to your needs (also can not fill in), fill in the content will be displayed in the app prompts the user whether to allow the location of alert content, the specific effect can be tested, there is no additional screenshots.
2. Call the positioning method before the need to call a function, directly in the above IOS7 initialization positioning service method inside the modification can be as follows:
-(void) Initializelocationservice {
Initializing the location Manager
_locationmanager = [[Cllocationmanager alloc] init];
Set up agents
_locationmanager.delegate = self;
Set positioning accuracy to M
_locationmanager.desiredaccuracy = Kcllocationaccuracybest;
Set Filter to None
_locationmanager.distancefilter = Kcldistancefilternone;
Start positioning
There are two ways to get location permissions, depending on your location usage
One is requestalwaysauthorization, one is requestwheninuseauthorization
[_locationmanager requestalwaysauthorization];//This sentence iOS8 above version use.
[_locationmanager startupdatinglocation];
}