This afternoon, hands-on iOS with the location, the results of the online read a lot of tutorials, but also to run the sample code directly, but has been unable to obtain the location, the code is as follows:
First import the corelocation.framework, and then introduce the header file #import <CoreLocation/CoreLocation.h>
Defining properties
@property (nonatomic, strong) Cllocationmanager *locationmanager;
Then use the proxycllocationmanagerdelegate
-(void) locate{//Determine if the location operation is allowed if ([Cllocationmanager locationservicesenabled]) {//positioning initialization _locationmana Ger=[[cllocationmanager alloc] init]; _locationmanager.delegate=self; _locationmanager.desiredaccuracy=kcllocationaccuracybest; _locationmanager.distancefilter=10; [_locationmanager startupdatinglocation];//Open Location}else {//Prompt user to not be able to locate operation Uialertview *alertview = [Uialert View alloc]initwithtitle:@ "Prompt" message:@ "location is not successful, make sure to turn on location" Delegate:nil cancelbuttontitle:@ "Cancel" otherbuttontitles:@ "OK ", nil]; [Alertview show]; }//start positioning [_locationmanager startupdatinglocation];} #pragma mark-corelocation delegate-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (NSArray * ) locations{///Here Locations stores the constant updated location coordinate value, takes the last value to the latest position, and if you do not want it to continue to update the location, after getting a value in this method, let Locationmanager Stopupdatinglocation cllocation *currentlocation = [locations Lastobject]; Get the current city name 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 city NSString *city = placemark.locality; if (!city) {//The city information of the four municipalities cannot be obtained by locality, but only by obtaining the province's method (if it is empty, it is known as the municipality) cities = Placemark.adm Inistrativearea; } cityname = city; NSLog (@ "Positioning complete:%@", cityname); The system will keep updating the data until you choose to stop the update, because we only need to get one latitude and longitude, so we stop updating [manager Stopupdatinglocation] after getting it. }else if (Error = = Nil && [array count] = = 0) {NSLog (@ "No results were returned."); else if (Error! = nil) {NSLog (@ "An error occurred =%@", error); } }];}
InViewdidloadAfter calling the locate has no data returned, the agent also does not execute, in the Internet to find a lot of tutorials, the code is much the same, but the results are not ideal, by chance to see the location of iOS8 and low version of the difference, my phone is just iOS8, then point to open that blog to see,Blog Please click here to enter
Here's a summary of the blog:
In previous versions of IOS8, we had no problem using cllocationmanager positioning, and recently in the IOS8 system it was not possible to locate .... This is a big problem!
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:
Locationmanager=[[cllocationmanager alloc] init]; locationmanager.delegate=self; Locationmanager.desiredaccuracy=kcllocationaccuracybest; locationmanager.distancefilter=10; if (iosversion>=8) { [Locationmanager requestwheninuseauthorization];//allows access to location data in the course of the program (IOS8 location required) } [Locationmanager startupdatinglocation];//open position
2. Add the following configuration in the Info.plist file:
(1) nslocationalwaysusagedescription
(2) Nslocationwheninuseusagedescription
After this is added, the positioning function will be able to use the normal!
original link:http://www.cnblogs.com/boyuanmeng/p/4211680.html
IOS Cllocationmanager positioning, IOS8 note