Two main classes of "iOS Dev-109" corelocation Framework Cllocationmanager and Clgeocoder Introduction

Source: Internet
Author: User

Corelocation framework mainly consists of two commonly used classes, one is Cllocationmanager, the other is Clgeocoder.


(1) The use of Corelocation, first import corelocation framework.


(2) The general use of the location manager to operate, that is, Cllocationmanager

--open, is startupdatinglocation; shut, it's stopupdatinglocation.

--can determine whether the location service is open locationservicesenabled, if not open, directly return empty, do not operate.

--IOS8 push message, need to get user permission, register the following push notification

--Position class cllocation has many properties, including direction course, and coordinates coordinate,coordinate have latitude and longitude attributes latitude and longitude.

#import "ViewController.h" #import <CoreLocation/CoreLocation.h> @interface Viewcontroller () < Cllocationmanagerdelegate> @property (Nonatomic,strong) Cllocationmanager *locmgr; @end @implementation viewcontroller-(Cllocationmanager *) locmgr{//judgment, the location service is turned on, if not turned on, directly return nil, do not perform the following operation if (![        Cllocationmanager locationservicesenabled]) return nil;    if (!_locmgr) {_locmgr=[[cllocationmanager alloc]init]; } return _locmgr;}    -(void) viewdidload {[Super viewdidload]; if ([[Uidevice currentdevice].systemversion doublevalue]>=8.0) {}//Set proxy self.locMgr.delegate    =self; [Self.locmgr startupdatinglocation];} -(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations{cllocation *loc=[    Locations Firstobject];    NSLog (@ "%f%f", loc.coordinate.latitude,loc.coordinate.longitude); If you only need to call 1 times, close immediately, because the default is to call this method every other minute, power consumption [manager stopupdatinglocation];} @end

--Personalized settings allowed by push notifications



(3) Calculate the straight line distance between two positions

    Cllocation *loc1=[[cllocation alloc]initwithlatitude:44 longitude:120];    Cllocation *loc2=[[cllocation alloc]initwithlatitude:45 longitude:120];    Cllocationdistance Distance=[loc1 DISTANCEFROMLOCATION:LOC2];    NSLog (@ "%f", distance);

The result is: 111122.007528

The properties that represent the location, and the elevation, can be used as a glimpse:

Cllocation *loc2=[[cllocation alloc]initwithcoordinate: (cllocationcoordinate2d) Altitude: (cllocationdistance) Horizontalaccuracy: (cllocationaccuracy) Verticalaccuracy: (cllocationaccuracy) Timestamp: (NSDate *)];

Of course, we can make a coordinate by ourselves and so on.


(4) There are also some properties for the location manager

    The self.locmgr.distancefilter=distance is updated once every distance    ;    Precision setting    self.locmgr.desiredaccuracy=kcllocationaccuracybestfornavigation;

(5) In iOS7, user authorization is required, but no additional developer settings are required, and pop-up windows are automatically available. However, in IOS8, although notifications for push messages are registered, they are still invalid and require special settings:

--First, the most important thing is to call the method requestwheninuseauthorization or requestalwaysauthorization, the difference is that One is to tell users to access location information only when the user is using the app, which is used regardless of whether you're using it or in the background. And the difference is that if you choose the latter, the information that comes out of the box is a custom message (that is, the personalization instructions above).

-(void) viewdidload {    [super viewdidload];    if ([[Uidevice currentdevice].systemversion doublevalue]>=8.0) {        [self.locmgr requestwheninuseauthorization] ;    }    [Self.locmgr startupdatinglocation];}

--of course, just calling the above statement is not enough. Also need to add the corresponding attributes in Info.plist, of course, two have been added is OK.



(6) If you do not need to care about the user's location information, simply to the latitude and longitude and the conversion between the address, it is geocoding and geographical coding.

Geocoding: Converts the name of a place into a latitude and a more detailed address. Anti-geocoding, is the input latitude and longitude, get the detailed address. The main code is as follows:

-(Ibaction) GeoCode: (ID) Sender {if (self.addressfield.text.length==0) return; [Self.geocoder geocodeAddressString:self.addressField.text completionhandler:^ (Nsarray *placemarks, Nserror *error)        {if (error) {[email protected] "could not find the detailed address you entered";            }else{Clplacemark *pm=[placemarks Firstobject];            Self.longitudelabel.text=[nsstring stringwithformat:@ "%.1f", Pm.location.coordinate.longitude];            Self.latitudelabel.text=[nsstring stringwithformat:@ "%.1f", Pm.location.coordinate.latitude];        Self.detailaddresslabel.text=pm.name; }    }];} -(Ibaction) Reversegeocode: (ID) Sender {if (self.reverselongitudefield.text.length==0 | |    self.reverselatitudefield.text.length==0) return; Cllocation *loc=[[cllocation Alloc]initwithlatitude:[self.reverselatitudefield.text DoubleValue] longitude:[    Self.reverseLongitudeField.text Doublevalue]]; [Self.geocoder reversegeocodelocation:loc completionhandler:^ (Nsarray *placemarks, NSerror *error) {if (error) {[email protected] "What is this latitude and longitude?"        ";            }else{Clplacemark *pm=[placemarks Firstobject];        Self.reversedetailaddresslabel.text=pm.name; }    }];}



(7) Supplementary Cllocationmanager of another knowledge, scope of monitoring. For example, 360 children watch the function of entering an area and leaving an area on the alert.

-Compliance with agreements <cllocationmanagerdelegate>

--Then, create a zone, the first method below has been reimbursed, now with the second method, that is clregion with clcircularregion to create this is not very easy to think.

Clregion *region=[[clregion alloc]initcircularregionwithcenter: (cllocationcoordinate2d) Radius: ( cllocationdistance) identifier:@ "China"]; Clregion *region=[[clcircularregion alloc]initwithcenter: (cllocationcoordinate2d) Radius: (CLLocationDistance) Identifier: (NSString *)];
--then, initiate the monitoring

[Self.locmgr startmonitoringforregion:region];

--Of course, there are proxy methods, that is, to enter this range or leave the time, do what things, in the proxy method do:

-(void) Locationmanager: (Cllocationmanager *) Manager didenterregion: (clregion *) region{    }-(void) Locationmanager: (Cllocationmanager *) Manager didexitregion: (clregion *) region{    }

--of course, the way to close the monitoring area is:

[Self.locmgr stopmonitoringforregion:region];

Two main classes of "iOS Dev-109" corelocation Framework Cllocationmanager and Clgeocoder Introduction

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.