ios - 在 iOS 中使用 Geocoder 类根据地址获取经纬度

标签 ios iphone objective-c geocoding

我根据经度和纬度值获得了当前位置,然后我还使用注释在谷歌地图上获得了多个位置。现在我想根据地址(即街道、城市和县)获取经度和纬度值。需要一些关于如何实现这一点的指导。

到目前为止,这是我尝试过的:-

#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

以上代码无法为我提供经纬度。需要一些帮助来了解我在这里做错了什么,或者我是否遗漏了什么。

提前致谢。

最佳答案

这是一个非常古老的答案,请检查新的更新

编辑:

在使用此检查 iOS8 更新之前

NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription

这是为了获取基于纬度和经度的用户区域,例如街道名称、州名称、国家/地区。

-(CLLocationCoordinate2D) getLocationFromAddressString: (NSString*) addressStr {
    double latitude = 0, longitude = 0;
    NSString *esc_addr =  [addressStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
    NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
    if (result) {
        NSScanner *scanner = [NSScanner scannerWithString:result];
        if ([scanner scanUpToString:@"\"lat\" :" intoString:nil] && [scanner scanString:@"\"lat\" :" intoString:nil]) {
            [scanner scanDouble:&latitude];
            if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && [scanner scanString:@"\"lng\" :" intoString:nil]) {
                [scanner scanDouble:&longitude];
            }
        }
    }
    CLLocationCoordinate2D center;
    center.latitude=latitude;
    center.longitude = longitude;
    NSLog(@"View Controller get Location Logitute : %f",center.latitude);
    NSLog(@"View Controller get Location Latitute : %f",center.longitude);
    return center;
    
}

在viewdidload方法中或者根据你的项目在某处调用这样的方法

[self getLocationFromAddressString:@"chennai"];

只需在您的浏览器中传递它 http://maps.google.com/maps/api/geocode/json?sensor=false&address=chennai

你将得到带有纬度和经度的 json 格式

http://maps.google.com/maps/api/geocode/json?sensor=false&address=@"your city name here"




 NSString *address = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@ %@ %@", self.streetField.text, self.cityField.text, self.countryField.text];

这个方法的用法....

CLLocationCoordinate2D center;
        center=[self getLocationFromAddressString:@"uthangarai"];
      double  latFrom=&center.latitude;
      double  lonFrom=&center.longitude;

  NSLog(@"View Controller get Location Logitute : %f",latFrom);
        NSLog(@"View Controller get Location Latitute : %f",lonFrom);

关于ios - 在 iOS 中使用 Geocoder 类根据地址获取经纬度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24503375/

相关文章:

ios - 如何在 iphone 应用程序开发中将数据从一个版本保存到另一个版本?

ios - 导航栏在动画期间不反射(reflect)新的文本属性到以前的 View Controller

ios - NSDate 下午 4 点以后

c# - Xamarin iOS - 模拟器 View 不刷新

ios - 在 iOS 5.0+ 中获取捕获视频的第一帧

iphone - UINavigationController 添加 UIView

iphone - 我可以为不同的应用程序提交相同的应用程序图标吗?

ios - 如何获取iPhone网络的信号强度(RSRP/RSSI/RSRQ/SINR),小区ID(LTE/3G/2G)

objective-c - 对于非 CF 类型,使用匹配的 CFBridging 调用来传入和传出 ARC 是否安全?

objective-c - 返回 C 数组和内存管理