【发布时间】:2014-08-21 14:33:40
【问题描述】:
我根据经度和纬度值获得了当前位置,然后我还使用注释在谷歌地图上获得了多个位置。现在我想根据地址(即街道、城市和县)获取经度和纬度值。需要一些关于如何实现这一点的指导。
到目前为止,这是我尝试过的:-
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize streetField = _streetField, cityField = _cityField, countryField = _countryField, fetchCoordinatesButton = _fetchCoordinatesButton, nameLabel = _nameLabel, coordinatesLabel = _coordinatesLabel;
@synthesize geocoder = _geocoder;
- (void)viewDidLoad
{
[super viewDidLoad];
_streetField.delegate=self;
_cityField.delegate=self;
_countryField.delegate=self;
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)fetchCoordinates:(id)sender {
NSLog(@"Fetch Coordinates");
if (!self.geocoder) {
NSLog(@"Geocdoing");
self.geocoder = [[CLGeocoder alloc] init];
}
NSString *address = [NSString stringWithFormat:@"%@ %@ %@", self.streetField.text, self.cityField.text, self.countryField.text];
NSLog(@"GET Addres%@",address);
self.fetchCoordinatesButton.enabled = NO;
[self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"Fetch Gecodingaddress");
if ([placemarks count] > 0) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSLog(@"GET placemark%@",placemark);
CLLocation *location = placemark.location;
NSLog(@"GET location%@",location);
CLLocationCoordinate2D coordinate = location.coordinate;
self.coordinatesLabel.text = [NSString stringWithFormat:@"%f, %f", coordinate.latitude, coordinate.longitude];
NSLog(@"CoordinatesLabel%@",self.coordinatesLabel.text);
if ([placemark.areasOfInterest count] > 0) {
NSString *areaOfInterest = [placemark.areasOfInterest objectAtIndex:0];
self.nameLabel.text = areaOfInterest;
NSLog(@"NameLabe%@",self.nameLabel.text);
}
}
self.fetchCoordinatesButton.enabled = YES;
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
@end
以上代码无法为我提供纬度和经度。需要一些帮助来了解我在这里做错了什么,或者我是否遗漏了什么。
提前致谢。
【问题讨论】:
-
使用这个链接是希望你stackoverflow.com/questions/21595538/…
-
@Anbu.Karthik 我需要根据地址获取经度和纬度值。您的代码基于邮政编码。我试过你的代码我将我的城市名称和国家名称传递给 url 但它不起作用请给我任何想法
标签: ios iphone objective-c geocoding