IOS Get current geographic coordinates
iOS Gets the current geographic coordinates, a few simple lines of code, but if you do not understand at first, there will also be some problems.
1. The library to be used to import the location: Corelocation.framwork. If you want to display the map, import mapkit.framwork.
2, add two lines in the Info.plist
Nslocationwheninuseusagedescription when the application needs to be positioned
Nslocationalwaysusagedescription Always use status (this line if not added, then the start time will not pop up to allow the use of the positioning prompt box, only manually set allow).
3. Import Class Library #import <corelocation/corelocation.h>, adding an agent cllocationmanagerdelegate. The basic setup is ready to be implemented directly by the code.
4, define an object, do some settings.
Self.locationmanager =[[Cllocationmanager alloc] init]; //set positioning accuracy, 10 m, hundred meters, bestself.locationmanager.desiredaccuracy=kcllocationaccuracybest; //how many meters per interval (where the setting is any movement)Self.locationManager.distanceFilter =Kcldistancefilternone; Self.locationmanager.Delegate= self;//Proxy Settings//start and always position if([Cllocationmanager locationservicesenabled]) {//The open location update needs to be polled with the server so it consumes power and shuts down with the Stopupdatinglocation method when it is not needed;[Self.locationmanager startupdatinglocation]; }Else{NSLog (@"please turn on the positioning function"); }
5, Agent method implementation
// This method is called when the location is turned on to determine if there is permission -(void) Locationmanager: (Cllocationmanager *) Manager Didchangeauthorizationstatus: (clauthorizationstatus) status{ if ([Cllocationmanager authorizationstatus]==kclauthorizationstatusnotdetermined) { // No permissions, invoke method open permission [Self.locationmanager requestalwaysauthorization]; } Else if ([Cllocationmanager authorizationstatus]==kclauthorizationstatusauthorizedwheninuse) { [Self.locationmanager startupdatinglocation];} }
6. Get positioning coordinates
//successful acquisition of latitude and longitude- (void) Locationmanager: (Cllocationmanager *) Manager didupdatetolocation: (cllocation *) newlocation fromlocation: ( Cllocation *) oldlocation{//get latitude and longitudeNSLog (@"Latitude:%f", NewLocation.coordinate.latitude); NSLog (@"Longitude:%f", NewLocation.coordinate.longitude); //Stop Location Update[manager stopupdatinglocation];}//Locate failure error message-(void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror: (Nserror *) error{NSLog (@"Error");}
Complete:
This is a simple geographical coordinate acquisition, if you want to use more features, routes, distances, pins, geocoding, etc. can be downloaded using the high-German or Baidu SDK, inside the head very detailed instructions, and simple method call!
IOS simple get current geographic coordinates