Geocoding and geo-coding

Source: Internet
Author: User
Tags reverse geocoding

#import <UIKit/UIKit.h>#import <CoreLocation/CoreLocation.h>@interface Viewcontroller:uiviewcontroller<cllocationmanagerdelegate>{    *_locationmanager;     * * * * *Addresstextfield;  -(Ibaction) findcurrentaddress: (ID) sender; -(Ibaction) findcoordinateofaddress: (ID) sender;

Reverse geocoding:

-(Ibaction) Findcurrentaddress: (ID) sender{if([Cllocationmanager locationservicesenabled]) {if(_locationmanager==Nil) {_locationmanager=[[Cllocationmanager alloc] init]; _locationmanager.distancefilter= -; _locationmanager.desiredaccuracy=kcllocationaccuracyhundredmeters; _locationmanager.Delegate=Self ; //for backward compatibility, set the deprecated purpose property//To the same as Nslocationusagedescription in the Info.plist_locationmanager.purpose =[[NSBundle Mainbundle] Objectforinfodictionarykey:@"nslocationusagedescription"];        } [_locationmanager startupdatinglocation]; Self.geocodingResultsLabel.text=@"Getting location ..."; }    Else{Self.geocodingResultsLabel.text=@"Location Services is unavailable"; }}
-(void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror: (Nserror *) error{if(Error.code = =kclerrordenied) {Self.geocodingResultsLabel.text=@"Location Information denied"; }}- (void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations{//Make sure the recent location eventCllocation *newlocation =[Locations Lastobject]; Nstimeinterval Eventinterval=[Newlocation.timestamp Timeintervalsincenow]; if(ABS (Eventinterval) <30.0)    {        //Make sure the event is valid        if(Newlocation.horizontalaccuracy <0)            return; //Instantiate _geocoder If it has not been already        if(_geocoder = =nil) _geocoder=[[Clgeocoder alloc] init]; //Only one geocoding instance per action//So stop any previous geocoding actions before starting this one        if([_geocoder isgeocoding]) [_geocoder Cancelgeocode]; [_geocoder reversegeocodelocation:newlocation Completionhandler:^ (nsarray* placemarks, nserror*error) {                if([Placemarks Count] >0) {Clplacemark*foundplacemark = [Placemarks objectatindex:0]; Self.geocodingResultsLabel.text=[NSString stringWithFormat:@"You is in :%@", foundplacemark.description]; }                Else if(Error.code = =kclerrorgeocodecanceled) {NSLog (@"Geocoding cancelled"); }                Else if(Error.code = =Kclerrorgeocodefoundnoresult) {Self.geocodingResultsLabel.text=@"No geocode result found"; }                Else if(Error.code = =Kclerrorgeocodefoundpartialresult) {Self.geocodingResultsLabel.text=@"Partial GeoCode Result"; }                Else{Self.geocodingResultsLabel.text=[nsstring stringWithFormat:@"Unknown Error:%@", error.description];                }            }         ]; //Stop updating location until they click the button again[manager Stopupdatinglocation]; }}

Geocoding:

-(Ibaction) Findcoordinateofaddress: (ID) sender{//Instantiate _geocoder If it has not been already    if(_geocoder = =nil) _geocoder=[[Clgeocoder alloc] init]; NSString*address =Self.addressTextField.text; [_geocoder geocodeaddressstring:address Completionhandler:^ (Nsarray *placemarks, Nserror *error) {            if([Placemarks Count] >0) {Clplacemark*placemark = [Placemarks objectatindex:0]; Self.geocodingResultsLabel.text=placemark.location.description; }            Else if(Error.domain = =Kclerrordomain) {                Switch(error.code) { CaseKCLErrorDenied:self.geocodingResultsLabel.text=@"Location Services Denied by User";  Break;  CaseKCLErrorNetwork:self.geocodingResultsLabel.text=@"No Network";  Break;  CaseKCLErrorGeocodeFoundNoResult:self.geocodingResultsLabel.text=@"No Result Found";  Break; default: Self.geocodingResultsLabel.text=error.localizeddescription;  Break; }            }            Else{Self.geocodingResultsLabel.text=error.localizeddescription; }        }     ];}

Practice requires attention:

1. Send only one geo-information encoding request at a time

2. If the user performs an action that leads to geocoding of the same location, then the result should be reused instead of requesting the same location multiple times

3. You should not send more than one geo-information encoding request within one minute. You should check whether the location of the user has moved significantly before calling another geo-information encoding request.

4, if you do not see the results, then please do not perform the geocoding request (for example, whether the application is running in the background)

Geocoding and geo-coding

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.