objective c如何获得给定坐标的地标

Gar*_*nik 5 objective-c reverse-geocoding ios

我有一个与反向地理编码有关的问题.

在我的应用程序中,我有一些坐标(不是我当前的坐标),我想将它们转换为地标.我挖了很多网站和代码,但它们都是关于当前位置的反向地理编码......

有没有办法获得指定坐标(不是当前位置)的地标?

如果有,请帮我一些代码或参考.

Gyp*_*psa 2

您可以通过两种方式实现这一目标:-

第一种方式:- 使用 google api 获取信息

-(void)findAddresstoCorrespondinglocation
{
    NSString *str = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",myCoordInfo.latitude,myCoordInfo.longitude];
    NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
    [request setRequestMethod:@"GET"];
    [request setDelegate:self];
    [request setDidFinishSelector: @selector(mapAddressResponse:)];
    [request setDidFailSelector: @selector(mapAddressResponseFailed:)];
    [networkQueue addOperation: request];
    [networkQueue go];

}
Run Code Online (Sandbox Code Playgroud)

作为响应,您将获得有关您指定的位置坐标的所有信息。

第二种方法:-

实施反向地理编码

a.)添加mapkit框架

MKReverseGeocoderb.)在.h文件中创建实例

MKReverseGeocoder *reverseGeocoder;
Run Code Online (Sandbox Code Playgroud)

c.) 在 .m 文件中

self.reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:cordInfo];
    reverseGeocoder.delegate = self;
    [reverseGeocoder start];
Run Code Online (Sandbox Code Playgroud)

实现两个委托方法MKReverseGeoCoder

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
    NSLog(@"MKReverseGeocoder has failed.");
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
    MKPlacemark * myPlacemark = placemark;
    NSString *city = myPlacemark.thoroughfare;
    NSString *subThrough=myPlacemark.subThoroughfare;
    NSString *locality=myPlacemark.locality;
    NSString *subLocality=myPlacemark.subLocality;
    NSString *adminisArea=myPlacemark.administrativeArea;
    NSString *subAdminArea=myPlacemark.subAdministrativeArea;
    NSString *postalCode=myPlacemark.postalCode;
    NSString *country=myPlacemark.country;
    NSString *countryCode=myPlacemark.countryCode;
    NSLog(@"city%@",city);
    NSLog(@"subThrough%@",subThrough);
    NSLog(@"locality%@",locality);
    NSLog(@"subLocality%@",subLocality);
    NSLog(@"adminisArea%@",adminisArea);
    NSLog(@"subAdminArea%@",subAdminArea);
    NSLog(@"postalCode%@",postalCode);
    NSLog(@"country%@",country);
    NSLog(@"countryCode%@",countryCode);

    }
Run Code Online (Sandbox Code Playgroud)