我试图获取用户当前位置的ZipCode。 它在美国运作良好,但其他国家也存在一些问题。
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
[self.locationManager requestAlwaysAuthorization];
}
_coder = [[CLGeocoder alloc] init];
_locationManager = [[CLLocationManager alloc]init];
_locationManager.delegate = self;
[_locationManager startUpdatingLocation];
//Block address
[_coder reverseGeocodeLocation: _locationManager.location completionHandler:
^(NSArray *placemarks, NSError *error) {
//Get address
CLPlacemark *placemark = [placemarks objectAtIndex:0];
if(placemark.addressDictionary == NULL){
NSLog(@"Error in getting address");
}else{
NSLog(@"Placemark array: %@",placemark.addressDictionary );
NSString *locateaddress;
//String to address
if(self.switcher.on){
locateaddress = [placemark.addressDictionary valueForKey:@"ZIP"];
if (!locateaddress || [locateaddress isKindOfClass:[NSNull class]]){
}
}else{
locateaddress = zipCodeString;
}
行NSLog(@"地标数组......)正在返回此..
> Placemark array: {
> City = Jerusalem;
> Country = Israel;
> CountryCode = IL;
> FormattedAddressLines = (
> "to begin south",
> Jerusalem,
> Israel
> );
> Name = "to begin south";
> State = Jerusalem;
> Street = "to begin south";
> Thoroughfare = "to begin south"; }
所以1)为什么没有邮政编码或邮政编码选项 2)如何从这些信息中获取邮政编码?
答案 0 :(得分:0)
试试这个。可能对你有所帮助。
//Get Location
-(void)updateLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
self.currentCLLocation = newLocation;
[self getCurrentAddress];
}
//Get address using location.
-(void)getCurrentAddress
{
//Geocoding Block
CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
CLLocation *currentLocation = [[CLLocation alloc] initWithLatitude:[self.sharedLat doubleValue] longitude:[self.sharedLong doubleValue]];
[geoCoder reverseGeocodeLocation: currentLocation completionHandler:
^(NSArray *placemarks, NSError *error)
{
//Get nearby address
self.placemark = [placemarks objectAtIndex:0];
//String to hold address
NSString *locatedAt = [[self.placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
// NSLog(@"City = %@",placemark.locality);
// NSLog(@"Country = %@",placemark.country);
self.currentCity = self.placemark.locality;
self.currentCountry = self.placemark.country;
//Print the location to console
NSLog(@"I am currently at %@ City",self.currentCity);
}];
}