iOS get current position latitude and longitude degree

Source: Internet
Author: User

Recently in doing a bus query project, you need to locate the current location in order to conduct a nearby site query, and you share how to obtain their current position latitude and longitude

First add the Corelocation.framework library:


Reference header file and declare cllocationmanagerdelegate agent:

Next, declare the variables you want to use:

@property (Strong, nonatomic) Cllocationmanager *locmanager;
@property (Strong, nonatomic) cllocation *checkinlocation;
@property (Strong, nonatomic) NSString *currentlatitude; Latitude
@property (Strong, nonatomic) NSString *currentlongitude;//Longitude

In Viewdidload, the user is determined whether the location service is enabled, and the system prompts for a positioning operation when the application is first entered. Then start locating the current location:

if ([Cllocationmanager locationservicesenabled]) {
        Self.locmanager = [[Cllocationmanager alloc] init];
        Self.locManager.delegate = self;
    } else{
        Uialertview *alert = [[Uialertview alloc] initwithtitle:@ ' warning ' message:@ ' unable to locate operation ' delegate:self cancelbuttontitle:@ "Determine" otherbuttontitles:nil, nil];
        [Alert show];
    }
    [Self.locmanager startupdatinglocation];

Invoke the Proxy method to update the current location:

-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations{
    self.checkinlocation = [Locations lastobject];
    Cllocationcoordinate2d cool = self.checkinLocation.coordinate;
    Self.currentlatitude  = [NSString stringwithformat:@ "%.4f", cool.latitude];
    Self.currentlongitude = [NSString stringwithformat:@ "%.4f", cool.longitude];
    
    NSLog (@ "%@,%@", self.currentlatitude,self.currentlongitude);
}

Run the program, the console also output the current position of latitude and longitude (cover the address, bad writing you can not find me):


Blog for the first time, I hope to be helpful to everyone, there are errors and deficiencies in the hope that we can make suggestions, learning the way to share with you, I was inspirational to become Mars Ape Man.

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.