IOS get to the user's current location _ios

Source: Internet
Author: User

Through corelocation positioning, get to the user's current position, with the map of different positioning.

First, import Corelocation.framework

Second, #import <CoreLocation/CoreLocation.h>

Third, the declaration agent <CLLocationManagerDelegate>

Four, code implementation

1. Statement

Cllocationmanager *locationmanager;//defines if the manager
//Determines whether the location operation is allowed
if ([Cllocationmanager Locationservicesenabled]) {
  Cllocationmanager *locationmanager = [[[Cllocationmanager alloc] init] autorelease];< C10/>self.locationmanager.delegate = self;
} else {
   //Prompt user Unable to locate operation
}

//start positioning
[Locationmanager startupdatinglocation];


2, update the position after the agent method, iOS6.0 the method

-(void) Locationmanager: (Cllocationmanager *) Manager 
  didupdatetolocation: (cllocation *) NewLocation 
      Fromlocation: (cllocation *) oldlocation {

 //latitude and lontitude are all nsstring variables
    //latitude
 self.latitude = [ NSString stringwithformat:@ "%.4f", newLocation.coordinate.latitude];

    Longitude
 self.longitude = [NSString stringwithformat:@ "%.4f",     newLocation.coordinate.longitude];
 
}


3, iOS6.0 above the recommended method of Apple

-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) Locations
{
  // Here locations stores the continuously updated location coordinate value, takes the last value as the newest position, and if you do not want it to continue to update the location, then get a value in this method and let Locationmanager stopupdatinglocation
  Cllocation *currentlocation = [locations lastobject];
  
  Cllocationcoordinate2d coor = currentlocation.coordinate;
  Self.latitude = Coor.latitude;
  Self.longitude = Coor.longitude;
  
  [Self.locationmanager stopupdatinglocation];
  
}

4, update the method of failure

-(void) Locationmanager: (Cllocationmanager *) Manager
    Didfailwitherror: (nserror *) Error {
 
 if (Error.code = = kclerrordenied) {
   ///Prompt user for error reason, can press option key Click Kclerrordenied to see more error message, printable Error.code value find cause
 }
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.