iOS geolocation use

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.