1 , overview
In iOS development, to join the 2 functions of map and location function, it must be developed based on 2 frameworks.
(1) Map Kit: for maps
(2) Core location: for geolocation
2 Hot Technical Terms:
Lbs:location Based Service
Solomo:social Local Mobile (Solomon)
2 , corelocation use of the framework
Corelocation Framework Usage Prerequisites:
Import Frame:
To import the primary header file:
#import <CoreLocation/CoreLocation.h>
Corelocation Framework Usage Notes:
The prefix for all data types in the Corelocation framework is CL
Using Cllocationmanager objects in corelocation to do user positioning
3 , Cllocationmanager
Common operations for Cllocationmanager:
(1) Start user positioning
-(void) startupdatinglocation;
(2) Stop user positioning
-(void) stopupdatinglocation;
When the Startupdatinglocation method is called, the user's location is constantly positioned, and the following method of the agent is called frequently in midstream:
-(void) Locationmanager: (Cllocationmanager *) Manager
Didupdatelocations: (Nsarray *) locations;
Locations The parameters contain cllocation object.
For example, remove the Location object:
Cllocation *loc = [locations Firstobject];
Print latitude and Longitude:
NSLog (@ "didupdatelocations------%f%f", Coordinate.latitude, Coordinate.longitude);
Remember to set up the agent before use:
Create a location Manager
Self.locmgr = [[Cllocationmanager alloc] init];
Set up Proxy
Self.locMgr.delegate = self;
4 , cllocation
Cllocation is used to represent geographic information for a location, such as latitude and longitude, elevation, and so on.
@property (readonly, nonatomic) Cllocationcoordinate2d coordinate;
Warp latitude
@property (readonly, nonatomic) cllocationdistance altitude;
Elevation
@property (readonly, nonatomic) Cllocationdirection course;
Course, course (value range is 0.0°~ 359.9°,0.0° represents true north direction)
@property (readonly, nonatomic) cllocationspeed speed;
Travel speed (unit m/s)
-(Cllocationdistance) Distancefromlocation: (const cllocation *) location calculates the distance between 2 locations
For example:
Calculates the straight line distance between 2 latitude and longitude degrees
Cllocation *loc1 = [[Cllocation alloc] initwithlatitude:40 longitude:116];
Cllocation *loc2 = [[Cllocation alloc] initwithlatitude:41 longitude:116];
Cllocationdistance distance = [Loc1 distancefromlocation:loc2];
NSLog (@ "Distance is%f", distance);
5 , Cllocationmanager
@property (Assign, nonatomic) cllocationdistance distancefilter;
How many meters to locate each time
@property (Assign, nonatomic) cllocationaccuracy desiredaccuracy;
Positioning accuracy (the more accurate it consumes)
6 , 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.
7 , protection of user privacy
The developer can set the nslocationusagedescription description for the purpose of positioning (privacy-location Usage Description) in Info.plist.
Once the user chooses "Don't allow", it means that your app will not be able to use the targeting feature later. For the sake of rigor, it is a good idea to determine if the current app's positioning function is available before using the location feature.
Cllocationmanager has a class method to determine if the current app's positioning function is available:
+ (BOOL) locationservicesenabled;
8 , Clgeocoder
Use Clgeocoder to complete geocoding and anti-geocoding:
geocoding : Obtain specific location information (such as latitude and longitude, the full name of the address, etc.) based on the given place name.
anti-geocoding : Obtain specific location information based on a given latitude and longitude.
Geo-coding methods:
-(void) geocodeaddressstring: (NSString *) addressstring Completionhandler: (clgeocodecompletionhandler) Completionhandler;
Anti-geocoding methods:
-(void) Reversegeocodelocation: (cllocation *) location Completionhandler: (clgeocodecompletionhandler) Completionhandler;
9 , Clgeocodecompletionhandler
When Geo-geocoding is complete, Clgeocodecompletionhandler is called:
typedef void (^clgeocodecompletionhandler) (Nsarray *placemarks, Nserror *error);
This block passes 2 parameters:
Error: There is a value when the encoding is wrong (for example, no specific information is encoded)
Placemarks: It contains Clplacemark objects.
Ten , Clplacemark
Clplacemark literally means a landmark, encapsulating detailed address location information.
@property (nonatomic, readonly) cllocation *location;
Location
@property (nonatomic, readonly) clregion *region;
Regional
@property (nonatomic, readonly) nsdictionary *addressdictionary;
Detailed address information
@property (nonatomic, readonly) NSString *name;
Address name
@property (nonatomic, readonly) NSString *locality;
City
iOS development corelocation (GPS positioning)