# 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