我是Objective-C编程的新手。
我想通过邮政编码在地图视图中显示位置。为此,我需要找到纬度和经度。
我已经参考了许多解决方案,通过将邮政编码传递给Web服务并获取所有信息的响应。
但我不想使用任何Web服务。我想使用CLGeocoder来完成。
我正在使用以下代码查找纬度和经度。
-(IBAction)fetchCoordinates:(id)sender {
if (!self.geocoder) {
self.geocoder = [[CLGeocoder alloc] init];
}
NSString *address = [NSString stringWithFormat:@"%@ %@ %@", self.streetField.text, self.cityField.text, self.countryField.text];
[self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"mark :: %@", placemarks);
if ([placemarks count] > 0) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
CLLocation *location = placemark.location;
CLLocationCoordinate2D coordinate = location.coordinate;
self.coordinatesLabel.text = [NSString stringWithFormat:@"%f, %f", coordinate.latitude, coordinate.longitude];
if ([placemark.areasOfInterest count] > 0) {
self.nameLabel.text = [placemark.areasOfInterest objectAtIndex:0];
} else {
self.nameLabel.text = @"No Area of Interest Was Found";
}
}
}];
}
如果我传递城市名称、国家名称或街道名称,此代码可以正常工作,但在传递邮政编码时无法正常工作。
提前致谢。