#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface Viewcontroller ()
/** geocoding Objects ***/
@property (nonatomic, strong) Clgeocoder *geocoder;
@property (Weak, nonatomic) Iboutlet Uitextfield *addressfield; Address
@property (Weak, nonatomic) Iboutlet UILabel *longitudelabel; Longitude
@property (Weak, nonatomic) Iboutlet UILabel *latitudelabel; Latitude
@property (Weak, nonatomic) Iboutlet UILabel *detaillabel; Specific address
@end
@implementation Viewcontroller
-(Ibaction) CODINGBTN: (ID) Sender {
0. Get the input location
NSString *addressstr = Self.addressField.text;
if (addressstr = = Nil | | addressstr.length = = 0) {
Loggreen (@ "Please enter Address");
Return
}
1. Create a Geocoding Object
2. Coding with geocoding objects
[Self.geocoder geocodeaddressstring:addressstr completionhandler:^ (Nsarray *placemarks, NSError *error) {
Placemarks: An array of landmarks, each of which contains information about the latitude and longitude of the location and the country code of the city area.
Gets the first information in the array
Clplacemark *placemark = [Placemarks firstobject];
Self.longitudeLabel.text = [NSString stringwithformat:@ "%f", Placemark.location.coordinate.longitude];
Self.latitudeLabel.text = [NSString stringwithformat:@ "%f", Placemark.location.coordinate.latitude];
Nsdictionary *addressdic = placemark.addressdictionary;
/**
* City = Beijing;
Country = China;
CountryCode = CN;
Formattedaddresslines = (
"Beijing China"
);
state = Beijing;
*/
Nsarray *addresses = addressdic[@ "Formattedaddresslines"];
nsmutablestring *mutstr = [nsmutablestring string];
For (NSString *substr in addresses) {
[Mutstr APPENDSTRING:SUBSTR];
}
Self.detailLabel.text = Mutstr; Simulator set to Chinese-output displayed in Chinese
Logred (@ "%@-%@-%f-%f", Placemark.name, Placemark.addressdictionary, Placemark.location.coordinate.longitude, Placemark.location.coordinate.latitude);
}];
Keyboard bounce back
[Self.view Endediting:yes];
}
/**
* 1. Create a Geocoding Object
*/
-(Clgeocoder *) geocoder{
if (!_geocoder) {
_geocoder = [[Clgeocoder alloc] init];
}
return _geocoder;
}
-(void) Viewdidload {
[Super Viewdidload];
Nslocationalwaysusagedescription
Nslocationwheninusedescription
}
Positioning-clgeocoder-Encoding