Introduction to iOS geolocation using latitude and longitude
English is not good, always inseparable from these two words, in this record a bit.
Longitude: Longitude, vertical line, (long has vertical meaning, used to denote longitude)
Latitude: Latitude,
Use
1. In the project import CoreLocation.framework
;
2. Request a user authorization (only required after iOS 8). Add a key to the Info.plist file:
Nslocationalwaysusagedescription or Nslocationwheninuseusagedescription. Where nslocationalwaysusagedescription is to always use location services, Nslocationwheninuseusagedescription is only used for location services in the foreground. The
corresponding authorization method is:
- (void)requestAlwaysAuthorization;- (void)requestWhenInUseAuthorization;
3. Initialize the code as follows:
//Create a geo-management object Self. Locationmanager = [[CllocationmanagerAllocInit];//Apply for authorization[ Self. Locationmanager Requestalwaysauthorization];//Settings Manager Delegate Self. locationmanager.delegate = Self;//Set accuracy Self. locationmanager.desiredaccuracy = Kcllocationaccuracybest;//Start Location Manager[ Self. Locationmanager Startupdatinglocation];
4. Implementing the Delegation method
//When geo-location updates occur- (void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray*) locations{//If multiple location updates occur in a relatively short period of time, these location updates may be reported all at once. Whenever the last item represents the current positionCllocation *newlocation = [locations Lastobject];//Show Latitude NSString*latitudestring = [NSStringstringWithFormat:@ "%g\u00b0", NewLocation. Coordinate. Latitude]; Self. Latitudelabel. Text= latitudestring;//Display longitude NSString*longitudestring = [NSStringstringWithFormat:@ "%g\u00b0", NewLocation. Coordinate. Longitude]; Self. Longitudelabel. Text= longitudestring;//Display horizontal accuracy NSString*horizontalaccuracystring = [NSStringstringWithFormat:@ "%GM", NewLocation. Horizontalaccuracy]; Self. Horizontalaccuracylabel. Text= horizontalaccuracystring;//Show Altitude NSString*altitudestring = [NSStringstringWithFormat:@ "%GM", NewLocation. Altitude]; Self. Altitudelabel. Text= altitudestring;//Display vertical accuracy NSString*verticalaccuracystring = [NSStringstringWithFormat:@ "%GM", NewLocation. Verticalaccuracy]; Self. Verticalaccuracylabel. Text= verticalaccuracystring;if(NewLocation. Verticalaccuracy<0|| NewLocation. Horizontalaccuracy<0) {return; }if(NewLocation. Horizontalaccuracy> -|| NewLocation. Verticalaccuracy> -) {return; }if( Self. Previouspoint==Nil) {return; }Else{ Self. Totalmovementdistance+ = [NewLocation distancefromlocation: Self. Previouspoint]; } Self. Previouspoint= NewLocation;NSString*distancestring = [NSStringstringWithFormat:@ "%GM", Self. Totalmovementdistance]; Self. Distancetraveledlabel. Text= distancestring;}//When acquiring a geo-location failure- (void) Locationmanager: (Cllocationmanager *) Manager Didfailwitherror: (Nserror*) error{NSString*errortype = (Error. Code= = kclerrordenied)?@ "Access Denied":@ "Unknown Error";Uialertview*alert = [[UialertviewAlloc] Initwithtitle:@ "Error getting location"Message:errortype Delegate:NilCancelbuttontitle:@ "Okay"Otherbuttontitles:Nil]; [Alert show];}
Code download
Https://github.com/limaofuyuanzhang/WhereAmI
Reference
Apps for iOS location services
Proficient in iOS Development (6th edition), chapter 19th Core location and map Kit
iOS geolocation use