IOS (map) cllocation

Source: Internet
Author: User

. h file #pragma mark---geocoding-(ibaction) GeoCode: (UIButton *) sender; @property (strong, nonatomic) iboutlet Uitextfield * Addresstextfield; @property (Strong, nonatomic) Iboutlet UILabel *longitudelabel; @property (Strong, nonatomic) Iboutlet UILabel *latitudelabel; @property (Strong, nonatomic) Iboutlet UILabel *detailaddress; #pragma mark----Anti-geocoding-(ibaction) Reversegetcode: (UIButton *) sender, @property (Strong, nonatomic) Iboutlet Uitextfield *longitudetextfield; @property ( Strong, Nonatomic) Iboutlet Uitextfield *latitudetextfiled; @property (Strong, nonatomic) Iboutlet UILabel *addressLabel ;

. m file

@interface Viewcontroller () <CLLocationManagerDelegate> @property (nonatomic,strong) Cllocationmanager * locmanager;//Positioning Management Object @property (Nonatomic,strong) Clgeocoder * geocoder;//Encoder Object @end

-(void) viewdidload {[Super viewdidload];  Determine if the iOS system version is greater than 8.0 to obtain authorization if ([[Uidevice currentdevice].systemversion doublevalue]>=8.0) {//Get authorization [     Self.locmanager Requestwheninuseauthorization];        }//Every few meters to locate once//self.locmanager.distancefilter=10;        Positioning accuracy (the more accurate the more power consumption)//self.locmanager.desiredaccuracy=kcllocationaccuracyhundredmeters; Start positioning [Self.locmanager startupdatinglocation];} 


#pragma mark----Implement Locationmanager's Proxy method #pragma mark----call (particularly high frequency) if you navigate to the location of the user//locations: Loaded with Calocation object-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations{        //1. Remove Position Object    Cllocation * Loc =[locations firstobject];        2. Remove the latitude    /longitude    double longitude=loc.coordinate.longitude;        Latitude    double latitude=loc.coordinate.latitude;        Print latitude    and longitude NSLog (@ "Longitude:%2.f Latitude:%2.f", longitude,latitude);        3. Print latitude    and longitude NSLog (@ "Start positioning!----%@", locations);    Stop positioning (power-saving measures: Stop Location Services immediately if you do not want to locate the service    [Self.locmanager stopupdatinglocation];} Calculates the straight line distance between 2 latitude and longitude-(void) countlinedistance{        cllocation * Cation1 =[[cllocation alloc]initwithlatitude:40 LONGITUDE:116];         Cllocation * Cation2 =[[cllocation alloc]initwithlatitude:41 longitude:116];        Cllocationdistance distance= [Cation1 distancefromlocation:cation2];        NSLog (@ "%f", distance);    }

Also add nslocationwheninuseusagedescription to the info file

Lazy Loading-(Cllocationmanager *) locmanager{        if (![ Cllocationmanager locationservicesenabled]) {        return nil;    }        Determine if location management exists if    (!_locmanager) {        //Create Location Manager        _locmanager =[[cllocationmanager Alloc]init];                _locmanager.delegate=self;    }        return _locmanager;    } -(Clgeocoder *) geocoder{        if (!_geocoder) {                _geocoder =[[clgeocoder alloc]init];    }    return _geocoder;}

Geocoding (latitude and longitude based on address)

-(Ibaction) GeoCode: (UIButton *) Sender {if ([Self.addressTextField.text length] ==0) {return;        } NSLog (@ "%@", Self.addressTextField.text); geocoding [Self.geocoder geocodeAddressString:self.addressTextField.text completionhandler:^ (Nsarray *placemarks, Nser Ror *error) {if (error) {//error (address disorderly input) [email protected] "The place name you entered has an error!!                   ";                              }else{Clplacemark * placemark =[placemarks firstobject];               Self.detailaddress.text=placemark.name;                                /* Name: Locality: City County: Country postancode: Zip code */self.longitudelabel.text=[nsstring stringwithformat:@ "%.2f", PLACEMARK.L               Ocation.coordinate.longitude];             Self.latitudelabel.text=[nsstring stringwithformat:@ "%2f", Placemark.location.coordinate.latitude];             }                  }]; }

Effect

Geo-anti-coding (location of addresses based on latitude and longitude)

Anti-geocoding-(Ibaction) Reversegetcode: (UIButton *) Sender {        NSString * longitude =self.longitudetextfield.text;    NSString * Latitude =self.latitudetextfiled.text;        if (longitude.length==0 | | latitude.length==0) {                return;    }        Cllocation * location =[[cllocation alloc]initwithlatitude:[latitude Doublevalue] longitude:[longitude DoubleValue]];        [Self.geocoder reversegeocodelocation:location completionhandler:^ (Nsarray *placemarks, NSError *error) {               if (error) {                         [email protected] "you entered the latitude and longitude error!";        } else{                       Clplacemark * pm=[placemarks Firstobject];                        self.addresslabel.text=pm.name;        }            }];}

Effects such as

IOS (map) cllocation

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.