iOS Learning _ map positioning and encoding and anti-coding

Source: Internet
Author: User

Positioning:

Introducing Header Files #import <CoreLocation/CoreLocation.h>
Claim Manager Properties:@property (Nonatomic,strong) Cllocationmanager *manager;
First step: Initialize the manager
Self.manager = [[Cllocationmanager alloc] init];
Step two: Make privacy judgments and authorize

//make a judgment of privacy    if(![Cllocationmanager locationservicesenabled]) {NSLog (@"whether to go to privacy settings, allow targeting"); }    //make a version of the Judgment    if([[[[Uidevice Currentdevice] systemversion] IntegerValue] >=8.0) {        //Determine authorization status        if([Cllocationmanager authorizationstatus]!=kclauthorizationstatusauthorizedwheninuse) {            //Request Authorization[Self.manager requestwheninuseauthorization]; }    }

You need to set what is allowed to be located in Inforplist before authorizing the request: Put Nslocationwheninuseusagedescription (Nslocationalwaysusagedescription this is another way) Enter into the inforplist, and then enter a sentence behind it;

Step three: Set agent and properties for manager

Self.manager. delegate = self;

Fourth step: Turn on positioning

    [Self.manager startupdatinglocation];

Implementing Proxy methods

//the proxy method is to start updating the location information after the positioning is successful, as long as the minimum distance of the moved setting starts to go this way;- (void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (nsarray<cllocation *> *) locations{//get the last positionCllocation *location =Locations.lastobject; //gets the coordinates of the positioncllocationcoordinate2d coordinate =location.coordinate; NSLog (@"Longitude:%f, Latitude:%f, Elevation:%f, direction of navigation:%f, walking speed:%f", Coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed); //in order to save power, if you do not use positioning, you need to turn off the positioning[Self.manager stopupdatinglocation];}//failed to locate- (void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror: (Nserror *) error{NSLog (@"failed to locate");}

Coding and anti-coding

Declaration encoding and anti-coding class:@property (Nonatomic,strong) Clgeocoder *geocoder;

Initialize object:self.geocoder = [[Clgeocoder alloc] init];

Get relevant information according to the place name ( code )

- (void) Getcoordinatebyadress: (NSString *) address{//Encoding Method[Self.geocoder geocodeaddressstring:address completionhandler:^ (nsarray<clplacemark *> * _Nullable Placemarks, Nserror *_nullable Error) {        //based on the returned landmarks, take out the first one (many of the landmarks are located)Clplacemark *mark =Placemarks.firstobject; //get location based on landmark (Mark)Cllocation *location =mark.location; //get area according to MarkClregion *region =mark.region; //Get dictionary informationNsdictionary *addressdic =mark.addressdictionary; NSLog (@"Landmark location:%@, area%@, location information%@", Location,region,addressdic); /*//nsstring *name=placemark.name;//place name//NSString *thoroughfare =placemark.thoroughfare;//Street//NSString *subthoroughfare=placemark.subthoroughfare; Street-related information, such as house numbers, etc.//NSString *locality=placemark.locality; City//NSString *sublocality=placemark.sublocality; City-related information, such as iconic buildings//NSString *administrativearea=placemark.administrativearea; State//NSString *subadministrativearea=placemark.subadministrativearea; Other administrative area information//NSString *postalcode=placemark.postalcode; Postcode//NSString *isocountrycode=placemark. Isocountrycode; Country code//NSString *country=placemark.country; Country//NSString *inlandwater=placemark.inlandwater; Water source, lake//NSString *ocean=placemark.ocean; Ocean//Nsarray *areasofinterest=placemark.areasofinterest; Associated or interest-related landmarks */    }];}

Remove address ( anti-coding ) based on latitude and longitude anti-coding

-(void  ) Getadressbylongitude: ( cllocationdegrees) Longitude Latitude: (cllocationdegrees) latitude{ //  anti-coding  //     create cllocation  Cllocation *location = [[Cllocation alloc] Initwithlatitude:latitude Longitude:    Longitude]; [Self.geocoder reversegeocodelocation:location Completionhandler:  ^ (Nsarray<clplacemark *> * _Nullable Placemarks, Nserror * _nullable error) {nsdictionary  *dic = Placemarks.firstObject.addressDictionary; NSLog ( @ "  anti-coding geolocation information:%@   " ,dic); }];}

Calculate the distance between two points

-(void) distance{    // Create position one    cllocation *locationbeijing = [[Cllocation Alloc] Initwithlatitude: Longitude:[];     *locationdaliang = [[Cllocation alloc] Initwithlatitude: Longitude:121];     = [locationbeijing Distancefromlocation:locationdaliang];    NSLog (@ " distance from Beijing to Dalian:%f", Distance);}

iOS Learning _ map positioning and encoding and anti-coding

Related Article

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.