The implementation of the IOS location feature

Source: Internet
Author: User

1. Import Frame

Add "Corelocation.framework" in Xcode

2. Import the main header file

#import <CoreLocation/CoreLocation.h>

3. Claims manager and Agent

@interface Viewcontroller () <CLLocationManagerDelegate> @property (nonatomic, strong) Cllocationmanager*   @end

4. Initialize the manager in Appdelegate or controller

//Location ManagerSelf.locationmanager=[[Cllocationmanager alloc]init]; if(![Cllocationmanager locationservicesenabled]) {Sslog (@"the location service may not be open at this moment, please set it open! "); //return;    }        //Request User Authorization If no authorization is granted    if([Cllocationmanager authorizationstatus]==kclauthorizationstatusnotdetermined) {[Self.locationmanager requestwheninuseauthorization];//get a location when you use the app//[Self.locationmanager requestalwaysauthorization];//Always get location    }    //Set up proxySelf.locationmanager.Delegate=Self ; //Set positioning accuracyself.locationmanager.desiredaccuracy=kcllocationaccuracybest; //Positioning frequency, positioning every few metersCllocationdistance distance =10.0;//10 m positioning onceSelf.locationmanager.distancefilter=distance; //Start Tracking Location[Self.locationmanager startupdatinglocation];

5. Agent method

//track the location agent method, which is executed each time the position changes (as long as it is positioned to the appropriate location)-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations{cllocation*location=[locations Firstobject];//Take the first position out of theCllocationcoordinate2d coordinate=location.coordinate;//position coordinatesSelf.coordinate =coordinate; Sslog (@"Longitude:%f, Latitude:%f, Elevation:%f, heading:%f, walking speed:%f", Coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed); //If you don't need real-time positioning, use it even if you turn off location services[Self.locationmanager stopupdatinglocation];}

6. Set in the Plist file

Nslocationwheninuseusagedescription later corresponds to the user's description when used

The implementation of the IOS location feature

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.