IOS simple get current geographic coordinates

Source: Internet
Author: User

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

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.