Corelocation basic usage

Source: Internet
Author: User
# Import "cjviewcontroller. H "# import <corelocation/corelocation. h> @ interface cjviewcontroller () <cllocationmanagerdelegate> @ property (nonatomic, strong) cllocationmanager * manager; @ end @ implementation cjviewcontroller // The Location Manager only needs one copy globally, so you only need to create it once and use the lazy load-(cllocationmanager *) manager {If (! _ Manager) {// 1. create a location manager (used to locate the user's location) _ manager = [[cllocationmanager alloc] init]; // After the location manager is created, the system will not automatically locate the location by default, because the location is relatively power-consuming, it does not automatically start locating. If you want to start user locating, you need to call the startupdatinglocation method // set the proxy to tell the user location _ manager. delegate = self;} return _ manager;}-(void) viewdidload {[Super viewdidload]; // checks whether the service is available (maybe when the program first accesses the user location, if ([cllocationmanager locationservicesenabled]) {// If the locating service can be used, start locating [self. manager startupdatinglocation]; // the location indicated by the proxy. // kcldistancefilternone is located every several meters, indicating that the user will be located once for any movement. If no movement is needed, the user will be located once, set a value self. manager. distancefilter = kcldistancefilternone; // The higher the accuracy, the higher the power consumption. manager. desiredaccuracy = kcllocationaccuracyhundredmeters; // indicates that the else is accurate within one hundred.} else {// the user's location cannot be located. // 1. tell the user to check the network status // 2. remind the user to turn on the positioning switch} [self countdistance];} // calculate the distance-(void) countdistance {cllocation * location1 = [[cllocation alloc] initwithlatitude: 39.5 longpolling: 136]; cllocation * location2 = [[cllocation alloc] initwithlatitude: 40 longpolling: 136]; cllocationdistance distance = [location1 distancefromlocation: location2]; nslog (@ "distance: % F ", distance) ;}# Pragma mark-cllocationmanagerdelegate // time-to-time monitoring (location) user location, frequent calls-(void) locationmanager :( cllocationmanager *) manager didupdatelocations :( nsarray *) locations {// each object in the array is a cllocation object. A cllocation represents a location cllocation * loc = [locations firstobject]; nslog (@ "% d", locations. count); // stop the update position. When you do not use the location service, you should immediately stop the location (update) because it consumes a lot of power. // [Manager stopupdatinglocation];} @ end

 

Corelocation basic usage

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.