iOS Development--Advanced article--geo-positioning corelocation

Source: Internet
Author: User

First, Corelocation

In the era of mobile internet, mobile apps can solve many of the user's life chores, such as
Nearby: Find a restaurant, find a KTV, find a movie theater, etc.
Navigation: Route planning based on user-defined start and end points, and guide users on how to reach

In the above applications, the positioning and mapping functions are used, in iOS development, to join the 2 major functions, must be based on 2 frameworks for development
Corelocation: For Geo-location, geocoding, area monitoring, etc. (focus on function implementation)
Mapkit: Used for map display, such as pin, route, overlay display, etc. (Focus on interface display)


2 Hot Technical Terms
Lbs:location Based Service
Solomo:social Local Mobile (Solomon)

Second, simple use

1, the use of corelocation framework
Corelocation Framework Usage Prerequisites
Import frame (can be omitted after Xcode5.0)

Import 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

2, Cllocationmanager
Common operations for Cllocationmanager


Start updating user locations
-(void) startupdatinglocation;

Stop Updating user locations
-(void) stopupdatinglocation;

When the Startupdatinglocation method is called, the user's location is constantly requested and refreshed, and the following method of the proxy is invoked once the request is made to the user's location
-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations;
The locations parameter contains the Cllocation object.

3. Cllocationmanager Supplement
For the sake of rigor, it's 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;

@property (Assign, nonatomic) cllocationdistance distancefilter;
How many meters to locate each time

@property (Assign, nonatomic) cllocationaccuracy desiredaccuracy;
Positioning accuracy (the more accurate it consumes)

4, Cllocation
Cllocation the geographic information used to represent a location, such as latitude and longitude, altitude, etc.
@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;
Movement speed (unit m/s)

Using-(Cllocationdistance) Distancefromlocation: (const cllocation *) Location method calculates the distance between 2 locations


Third, IOS 8.0 adaptation and iOS9.0 supplement

1. Protection of user's privacy
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
To get the user's location
Want to access users ' contacts, calendars, cameras, albums, and more

When you want to access the user's privacy information, the system will automatically pop up a dialog box to let the user authorize

Once the user chooses "Don't allow", it means your app won't be able to use the targeting feature

Developers can set nslocationusagedescription instructions for positioning in Info.plist (privacy-location Usage Description)

2. Location adaptation of IOS 8.0+
Starting with iOS 8.0, Apple has further enhanced the protection of user privacy.
When the app wants to access the user's privacy information, the system no longer automatically pops up a dialog box to allow the user to authorize

Solution:
(1) Call the iOS 8.0 API to proactively request user authorization
-(void) requestalwaysauthorization//Request permission to access the user's location on both front and back desk
-(void) requestwheninuseauthorization//Request permission to obtain a user's location at the front desk

(2) Be sure to configure the corresponding key value in the Info.plist file, otherwise the above request authorization method does not take effect
Nslocationalwaysusagedescription: Allow the description of GPs in front of the background
Nslocationwheninusedescription: Allow GPS description in the foreground


3. IOS 9.0 Location Supplement
IOS 9.0 If you are currently in the foreground authorization state, the default is not to get the user location later. But you can set the following property to Yes to continue to get the background position, but a blue bar will appear
@property (Assign, nonatomic) BOOL allowsbackgroundlocationupdates
Use note: The corresponding background mode must be set: Location updates


IOS 9.0 can request a user location one time
-(void) requestlocation
-(void) Locationmanager: (nonnull cllocationmanager *) Manager didupdatelocations: (nonnull nsarray<cllocation *> *) Locations//Successful call
-(void) Locationmanager: (nonnull cllocationmanager *) Manager didfailwitherror: (nonnull nserror *) Error//Failed call


Iv. Latitude and longitude

1, 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

2, latitude and longitude degree
Prime Meridian
Through the Greenwenger Observatory in London
Go East (right), it's longitude (E)
Go West (to the left), it's longitude (W)
East and West 180 °, total 360 °

Equator
Go North (top), Latitude (N)
Go south (below), it's southern latitude (S)
South latitude 90 °, a total of 180 °

Across longitude \ Latitude Greater (1°≈111km)
The greater the scope of the representation
The smaller the thing you see on the map.

3. Latitude and longitude range of celestial Kingdom
Latitude range: N 3°51′~ n 53°33′
Longitude range: E 73°33′~ e 135°05′
Beijing e116°28 ' n39°54 '
Guangzhou e113°15 ' n23°08 '
Shenzhen e113°46 ' n22°27 '


Five, (reverse) geocoding

1, Clgeocoder
Use Clgeocoder to complete geocoding and anti-geocoding
Geocoding: Obtain specific location information (such as latitude and longitude, full name of address, etc.) based on a 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;

2, Clgeocodecompletionhandler
When Geo-geocoding is complete, the 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.

3, 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--Advanced article--geo-positioning corelocation

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.