Through corelocation positioning, get to the user's current position, with the map of different positioning.
First, import Corelocation.framework
Second, #import <CoreLocation/CoreLocation.h>
Third, the declaration agent <CLLocationManagerDelegate>
Four, code implementation
1. Statement
Cllocationmanager *locationmanager;//defines if the manager
//Determines whether the location operation is allowed
if ([Cllocationmanager Locationservicesenabled]) {
Cllocationmanager *locationmanager = [[[Cllocationmanager alloc] init] autorelease];< C10/>self.locationmanager.delegate = self;
} else {
//Prompt user Unable to locate operation
}
//start positioning
[Locationmanager startupdatinglocation];
2, update the position after the agent method, iOS6.0 the method
-(void) Locationmanager: (Cllocationmanager *) Manager
didupdatetolocation: (cllocation *) NewLocation
Fromlocation: (cllocation *) oldlocation {
//latitude and lontitude are all nsstring variables
//latitude
self.latitude = [ NSString stringwithformat:@ "%.4f", newLocation.coordinate.latitude];
Longitude
self.longitude = [NSString stringwithformat:@ "%.4f", newLocation.coordinate.longitude];
}
3, iOS6.0 above the recommended method of Apple
-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) Locations
{
// Here locations stores the continuously updated location coordinate value, takes the last value as the newest position, and if you do not want it to continue to update the location, then get a value in this method and let Locationmanager stopupdatinglocation
Cllocation *currentlocation = [locations lastobject];
Cllocationcoordinate2d coor = currentlocation.coordinate;
Self.latitude = Coor.latitude;
Self.longitude = Coor.longitude;
[Self.locationmanager stopupdatinglocation];
}
4, update the method of failure
-(void) Locationmanager: (Cllocationmanager *) Manager
Didfailwitherror: (nserror *) Error {
if (Error.code = = kclerrordenied) {
///Prompt user for error reason, can press option key Click Kclerrordenied to see more error message, printable Error.code value find cause
}
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.