iOS-CoreLocation: I know where you are!

iOS-CoreLocation: I know where you are!

1. Positioning

Steps:

Create a CLLocationManager instance and keep a strong reference to it

Set the CLLocationManager delegate to listen for and get the updated location

Start location update

  1. _manager?=?[[CLLocationManager?alloc]?init];
  2. _manager.delegate?=?self;
  3. [_manager?startUpdatingLocation];

Since in iOS8, developers need to actively request authorization from the system, the following steps are required in iOS8 and above:

Set NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription in info.plist file

Use [_manager requestWhenInUseAuthorization] to request authorization in the code

Implement the Manager's proxy method didChangeAuthorizationStatus: to determine whether to start location updates based on the status

Parameter analysis

In the Manager's proxy method locationManager:didUpdateLocations:, the locations parameter passed in is of type CLLocation.

The main parameters of the CLLocation method are:

  1. //Longitude and latitude  
  2. @property (readonly,?nonatomic)?CLLocationCoordinate2D?coordinate;
  3. //sea level  
  4. @property (readonly,?nonatomic)?CLLocationDistance?altitude;
  5. //speed  
  6. @property (readonly,?nonatomic)?CLLocationSpeed?speed
  7. //Current timestamp  
  8. @property (readonly,?nonatomic,?copy)?NSDate?*timestamp;

2. Direction

How to use

The same three steps as positioning, the difference is that no authorization is required to obtain the direction

  1. _manager?=?[[CLLocationManager?alloc]?init];
  2. _manager.delegate?=?self;
  3. [_manager?startUpdatingHeading];

Parameter analysis

In the Manager's proxy method locationManager:didUpdateHeading:, the newHeading parameter passed in is of type CLHeading.

The main parameters of the CLHeading method are:

  1. //Declination from magnetic north  
  2. @property (readonly,?nonatomic)?CLLocationDirection?magneticHeading;
  3. //Declination angle from true north  
  4. @property (readonly,?nonatomic)?CLLocationDirection?trueHeading;

3. Regional monitoring

How to use

It also requires roughly three steps, the first two of which are the same as positioning, and the third step is to create a range:

  1. _manager?=?[[CLLocationManager?alloc]?init];
  2. _manager.delegate?=?self;
  3. if ?([[UIDevice?currentDevice].systemVersion?doubleValue]?>=? 8.0 )?{
  4. ???[_manager?requestAlwaysAuthorization];
  5. }
  6. CLLocationCoordinate2D?coordinate?=?CLLocationCoordinate2DMake( 32.656688 ,? 110.74677 );
  7. CLCircularRegion?*circular?=?[[CLCircularRegion?alloc]?initWithCenter:coordinate?radius: 1000 ?identifier:@ "bourne" ];
  8. [_manager?startMonitoringForRegion:circular];

Proxy method (one in, one out)

  1. //Called when entering the scope  
  2. -?( void )locationManager:(CLLocationManager?*)manager?didEnterRegion:(CLRegion?*)region?{
  3. ????NSLog(@ "I'm in!" );
  4. }
  5. //Called when leaving the scope  
  6. -?( void )locationManager:(CLLocationManager?*)manager?didExitRegion:(CLRegion?*)region?{
  7. NSLog(@ "I'm out!" );
  8. }

HELP: It seems that it doesn't work in iOS8.3, neither on real devices nor on simulators, but works fine on iOS7.1! I don't know what's going on, if anyone knows please tell me. Thank you.

4. Geocoding & De-geocoding

Geocoding means that you give it a place name and it returns the latitude and longitude of the place; reverse geocoding means that you give it a longitude and latitude and it returns a place name. If you do not use the positioning function, you do not need authorization.

Geocoding

  1. _coder?=?[[CLGeocoder?alloc]?init];
  2. [_coder?geocodeAddressString:@ "Hubei Automotive Industry College" ?completionHandler:^(NSArray?*placemarks,?NSError?*error)?{
  3. ???CLPlacemark?*marks?=?placemarks.firstObject;
  4. ???NSLog(@ "%f?-?%f" ,?marks.location.coordinate.latitude,?marks.location.coordinate.longitude);
  5. }];

There are many available properties in CLPlacemark, you can go in and take a look.

Reverse Geocoding

  1. CLLocation?*loc?=?[[CLLocation?alloc]?initWithLatitude: 32.656688 ?longitude: 110.74677 ];
  2. [_coder?reverseGeocodeLocation:loc?completionHandler:^(NSArray?*placemarks,?NSError?*error)?{
  3. ??? for ?(CLPlacemark?*mark?in?placemarks)?{
  4. ???????NSLog(@ "%@" ,?mark.name);
  5. ???}
  6. }];

It is relatively simple to implement, the key lies in how to use this data!

Extensions

CoreLocation is still relatively troublesome to use, requiring authorization, judging the system version, etc., so it is recommended to use a third-party framework, such as: LocationManager is very good, using Block, it is very simple!

<<:  Winning in design, high-end and classy or low-key and luxurious with connotation – iteration vs. planning

>>:  Foreign media comprehensive interpretation: iOS 9 everything we must know

Recommend

Liu Heng's Physiognomy and Five Elements Personality

Liu Heng's Physiognomy and Five Elements Pers...

Why do some people have no pores? They are just smaller

The simplest meaning of pore is "hole where ...

How can brands crack the problem of “private domain traffic”?

A few months ago, when I was chatting with an inv...

Is Google's return to China a pipe dream or a comeback?

On October 20, American Internet giant Google ann...

Seeing too much, China's Sky Eye poses a problem for scientists

"The main challenge facing the China Sky Eye...

Youdao Kaoshen English Level 4 and 6 Full Course Baidu Cloud Download

Youdao Kaoshen English Level 4 and 6 full-course ...

TBchoi Human Body Basics Class 8th 2020 LAELAPS [Good quality with brushes]

TBchoi Human Body Basics Class 8th 2020 LAELAPS [...

Yawning is contagious! When people yawn, do animals yawn too?

When we see someone yawning, we often yawn involu...

Marketing planning and promotion: How to create Labor Day H5?

The May Day holiday is a hot topic, so brands cer...

The Simplest Rule for Super Users: How to Achieve User Growth?

2018 is the second half of the Internet, and it i...

Hot marketing promotion calendar for the second half of 2019!

After a busy June The second half of 2019 Also ca...