1. IntroductionCoreLocation.framework 添加委托CLLocationManagerDelegate
2.
{
float Currentlat,currentlon;
Cllocationmanager *locationmanager;
UILabel *citylabel;
}
Add in 3.viewDidLoad
if ([cllocationmanager locationservicesenabled] = = NO) {
NSLog (@ "No GPS service");
Uialertview *alertview = [[Uialertview alloc]initwithtitle:@ "Warning" message:@ "no GPS service" Delegate:self cancelButtonTitle:@ "Cancellation" otherbuttontitles:nil, Nil];
[Alertview show];
}else{
Cllocationmanager *locmanager = [[Cllocationmanager alloc]init];//first defines an instance of Cllocationmanager
Locationmanager=locmanager;
[Locationmanager requestalwaysauthorization];//corresponds to the Nslocationalwaysusagedescription key in Info.plist
[Locmanager setdelegate:self]; Set the proxy for itself
[Locmanager setdesiredaccuracy:kcllocationaccuracybest];//set accuracy to the most accurate
Locmanager.distancefilter = 1000.0f;
[Locationmanager startupdatinglocation];
}
4.
ios6.0 or more
-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations{
Cllocation *newlocation = [locations Lastobject];
Cllocationcoordinate2d coor = newlocation.coordinate;
Currentlat = Coor.latitude;
Currentlon = Coor.longitude;
NSLog (@ "Current dimension iOS7%f current Longitude%f", Currentlat,currentlon);
Clgeocoder * GeoCoder = [[Clgeocoder alloc] init];
[GeoCoder reversegeocodelocation:newlocation completionhandler:^ (Nsarray *placemarks, Nserror *error) {
For (Clplacemark * Placemark in Placemarks) {
Nsdictionary *test = [Placemark addressdictionary];
Country (country) state (city) sublocality (district)
Citylabel.text=[test objectforkey:@ "state"];
}
}];
[Manager Stopupdatinglocation];
}
-(void) Locationmanager: (Cllocationmanager *) Manager
Didfailwitherror: (Nserror *) error
{
NSLog (@ "failed to locate");
Uialertview *alert=[[uialertview alloc]initwithtitle:@ "Prompt" message:@ "location failed" Delegate:nil cancelbuttontitle:@ "OK" Otherbuttontitles:nil];
[Alert show];
}
5.
iOS8 must NOTE:
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;
Add the following sentence before running the start update
[Locationmanager requestalwaysauthorization]; The Nslocationalwaysusagedescription key in the relative info.plist
2. Add the following configuration in the Info.plist file:
(1)NSLocationAlwaysUsageDescription
(2)NSLocationWhenInUseUsageDescription
The value of these two keys is the description of the authorization alert, and the example is configured as follows [ 勾选Show Raw Keys/Values后进行添加 ]:
Using corelocation positioning in iOS8