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
Defining properties
?
1 |
@property (nonatomic , strong)CLLocationManager *locationManager; |
Then use the proxy cllocationmanagerdelegate
- (void) locate{//determine if the location operation is allowed if([Cllocationmanager locationservicesenabled]) {//Positioning Initialization_locationmanager=[[Cllocationmanager alloc] init]; _locationmanager.Delegate=Self ; _locationmanager.desiredaccuracy=kcllocationaccuracybest; _locationmanager.distancefilter=Ten; [_locationmanager startupdatinglocation];//Turn on positioning}Else { //prompting the user for no location actionUialertview *alertview = [[Uialertview alloc]initwithtitle:@ prompt message:@ location not successful, please confirm to turn on locationDelegate: nil cancelbuttontitle:@ cancel otherbuttontitles:@ ok, nil]; [Alertview show]; } //Start Positioning[_locationmanager startupdatinglocation];} #pragmaMark-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 keep updating the location, then get a value in this method and let Locationmanager stopupdatinglocationCllocation *currentlocation =[Locations Lastobject]; //get the name of your current cityClgeocoder *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 cityNSString *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; } CityName=City ; NSLog (@ location Complete:%@,cityname); //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); } }];}
After the call in the viewdidload Locate has no data returned, the agent 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 iOS8 location and low version of the difference, my phone is just iOS8 , then click on the 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
?
1 |
- ( void )requestAlwaysAuthorization; |
(2) Allow access to location data during use of the application
?
1 |
- ( void )requestWhenInUseAuthorization; |
Examples such as the following
Locationmanager=[[Cllocationmanager alloc] init]; Locationmanager. delegate= self; Locationmanager.desiredaccuracy=kcllocationaccuracybest; Locationmanager.distancefilter=ten; if (iosversion>=8) { [Locationmanager requestwheninuseauthorization]; // allow access to location data in the course of the program (iOS8 positioning required) } [Locationmanager startupdatinglocation]; // Turn on positioning
2. Add the following configuration in the Info.plist file:
(1) nslocationalwaysusagedescription
(2) Nslocationwheninuseusagedescription
IOS Cllocationmanager positioning, IOS8 note