Recently in doing a bus query project, you need to locate the current location in order to conduct a nearby site query, and you share how to obtain their current position latitude and longitude
First add the Corelocation.framework library:
Reference header file and declare cllocationmanagerdelegate agent:
Next, declare the variables you want to use:
@property (Strong, nonatomic) Cllocationmanager *locmanager;
@property (Strong, nonatomic) cllocation *checkinlocation;
@property (Strong, nonatomic) NSString *currentlatitude; Latitude
@property (Strong, nonatomic) NSString *currentlongitude;//Longitude
In Viewdidload, the user is determined whether the location service is enabled, and the system prompts for a positioning operation when the application is first entered. Then start locating the current location:
if ([Cllocationmanager locationservicesenabled]) {
Self.locmanager = [[Cllocationmanager alloc] init];
Self.locManager.delegate = self;
} else{
Uialertview *alert = [[Uialertview alloc] initwithtitle:@ ' warning ' message:@ ' unable to locate operation ' delegate:self cancelbuttontitle:@ "Determine" otherbuttontitles:nil, nil];
[Alert show];
}
[Self.locmanager startupdatinglocation];
Invoke the Proxy method to update the current location:
-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations{
self.checkinlocation = [Locations lastobject];
Cllocationcoordinate2d cool = self.checkinLocation.coordinate;
Self.currentlatitude = [NSString stringwithformat:@ "%.4f", cool.latitude];
Self.currentlongitude = [NSString stringwithformat:@ "%.4f", cool.longitude];
NSLog (@ "%@,%@", self.currentlatitude,self.currentlongitude);
}
Run the program, the console also output the current position of latitude and longitude (cover the address, bad writing you can not find me):
Blog for the first time, I hope to be helpful to everyone, there are errors and deficiencies in the hope that we can make suggestions, learning the way to share with you, I was inspirational to become Mars Ape Man.