a simple explanation
1.CLLocationManager
Common operations and properties of Cllocationmanager
Start user positioning-(void) startupdatinglocation;
Stop user positioning-(void) stopupdatinglocation;
Note: When the Startupdatinglocation method is called, the user's location is started constantly, and the agent's method is called frequently in the middle
-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations;
How many meters to locate each time
@property (Assign, nonatomic) cllocationdistance distancefilter;
Positioning accuracy (the more accurate it consumes)
@property (Assign, nonatomic) cllocationaccuracy desiredaccuracy;
2.CLLocation
Cllocation the geographic information used to represent a location, such as latitude and longitude, altitude, etc.
(1) Latitude
@property (readonly, nonatomic) Cllocationcoordinate2d coordinate;
(2) Altitude
@property (readonly, nonatomic) cllocationdistance altitude;
(3) Course, course (value range is 0.0°~ 359.9°,0.0° represents true north direction)
@property (readonly, nonatomic) Cllocationdirection course;
(4) Walking speed (unit m/s)
@property (readonly, nonatomic) cllocationspeed speed;
(5) Calculate the distance between 2 positions
-(Cllocationdistance) Distancefromlocation: (const cllocation *) Location method
3.cllocationcoordinate2d
Cllocationcoordinate2d is a structure used to represent latitude and longitude, defined as follows
typedef struct {
Cllocationdegrees latitude; Latitude
Cllocationdegrees longitude; Longitude
} cllocationcoordinate2d;
The Cllocationcoordinate2dmake function is generally used to create cllocationcoordinate2d
Second, code example
1//2//YYVIEWCONTROLLER.M 3//18-Location Services 4//5//Created by Apple on 14-8-9. 6//Copyright (c) 2014 Yangyong. All rights reserved. 7//8 9 #import "YYViewController.h" #import <corelocation/corelocation.h>11 12// Required to comply with the Cllocationmanagerdelegate agreement @interface Yyviewcontroller () <cllocationmanagerdelegate>14 @property ( Nonatomic,strong) Cllocationmanager *locmgr;15 @end16 @implementation YYViewController18 #pragma mark-lazy load 19-( Cllocationmanager *) LocMgr20 {22//1. Create Location Manager (locate user's location) self.locmgr=[[cllocation Manager alloc]init];24//2. Set Agent self.locmgr.delegate=self;26}27 return _locmgr;28}29-(void) v iewDidLoad30 {"Super Viewdidload];32 33//Determine if the user location service is turned on if ([Cllocationmanager locationservicesenabled ] {35//start locating the user's location [self.locmgr startupdatinglocation];37//per how many meters (here is set for any move) the SEL f.locmgr.distancefilter=kcldistancefilternone;39Set positioning accuracy, the higher the general accuracy, the more power consumption (here is set to the highest precision, suitable for navigation applications) self.locmgr.desiredaccuracy=kcllocationaccuracybestfornavigation; }else42 {//Cannot locate the user's location 43//1. Reminds the user to check the current network condition 44//2. Alert the user to open the position switch 45}46 47//test method, calculate two positions between Distance [self countdistance];49}50 #pragma mark-cllocationmanagerdelegate52/**53 * When positioned to the user's location, it will be called (frequency of the call is more frequent) 54 * /55-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) LOCATIONS56 {57/// The locations array contains Cllocation objects, and a Cllocation object represents a position of cllocation *loc = [Locations firstobject];59 60//Dimension: Loc . Coordinate.latitude61//Longitude: loc.coordinate.longitude62 NSLog (@ "Latitude =%f, Longitude =%f", Loc.coordinate.latitude, Loc.coordinate.longitude); NSLog (@ "%d", locations.count); 64 65//Stop Update location (if the location service does not need to be updated in real-time, then you should stop updating) 66// [Self.locmgr stopupdatinglocation];67 68}69 70//calculate distance between two positions-(void) CountDistance72 {73//Create two-position objects based on latitude and longitude Cllo cation *loc1=[[cllocation Alloc]initwithlatitude:40 Longitude:116];75 cllocation *loc2=[[cllocation alloc]initwithlatitude:41 longitude:116];76//calculates the distance between two locations CLLoc Ationdistance Distance=[loc1 distancefromlocation:loc2];78 NSLog (@ "(%@) and (%@) distance =%fm", loc1,loc2,distance); 79}80 81 @ End
Print View:
Code Description:
1. About Proxy methods
You need to set up the proxy, tell the user the current location through the proxy, there are two proxy methods:
The locations parameter contains the Cllocation object.
The latter is an outdated method, in which an array is substituted in the new method (the first one). Note: This method is called when it is positioned to the user's location and is frequently called: do not use local variables (Create location manager) because the local variables are destroyed when the method ends. It is recommended to use a global variable and create it once (using lazy loading). 2. Positioning accuracy 3. If you find that your location service is not open, you should alert the user to the location Services feature. 4. Location service is relatively power consumption, if it is to do location services (no need to update in real-time), then the user location, should stop the update location.
third, the protection of user privacy
1. Permission Settings description
Starting with iOS 6, Apple has done a great job of protecting the privacy of its users, and the following must be authorized by user approval
(1) To get the user's location
(2) Want to access the user's address book, Calendar, camera, album, etc.
When you want to access the user's privacy information, the system will automatically pop up a dialog box to let the user authorize
Note: Once the user chooses "Don't allow", it means that your app will not be able to use the targeting feature later, and when the user chooses it for the first time, it will never be reminded again.
Therefore, in the program should be judged, if you find that your location service is not open, then you should remind users to open the location services function.
Cllocationmanager has a class method to determine if the current application positioning function is available + (BOOL) locationservicesenabled;
Common method: Tell the user how to open the authorization
2. Developers can set nslocationusagedescription instructions for positioning in Info.plist (privacy-location Usage Description)
Description: The location service here is network-based. Typically, location services can be based on a GPS, base station, or network.
iOS development and expansion-corelocation location Services