【问题标题】:objective c how to get placemarks of given coordinates目标c如何获取给定坐标的地标
【发布时间】:2012-01-27 09:40:31
【问题描述】:

我有一个关于反向地理编码的问题。

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

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

如果有,请帮我提供一些代码或参考资料。

【问题讨论】:

  • 什么意思?无论该位置是否属于您,您都必须做同样的事情。如果您正在为 iOS 5 开发,这里有一些参考:developer.apple.com/library/ios/#documentation/CoreLocation/…
  • 您不能将位置分配给 CLLocation.coordinat.longitude 或 latitude。并且 reverseGeoCoding 方法只接受 CLLocation 而不是 CLLocationCoordinate2D
  • CLLocation *loc = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];

标签: objective-c ios reverse-geocoding


【解决方案1】:

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

第一种方式:- 使用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];

}

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

第二种方法:-

实施反向地理编码

a.)添加 mapkit 框架

b.)在 .h 文件中创建 MKReverseGeocoder 的实例

MKReverseGeocoder *reverseGeocoder;

c.)在.m文件中

self.reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:cordInfo];
    reverseGeocoder.delegate = self;
    [reverseGeocoder start];

实现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);

    }

【讨论】:

  • 请注意:在 iOS 5.0 中,它已被弃用,取而代之的是 GLGeocoder class: developer.apple.com/library/ios/#documentation/CoreLocation/…
  • 我建议使用google api方法,简单可靠。
  • 谢谢 Gypsa 我会尝试第一种方法,但我知道 MKReverseGeoCoder 在 iOS 5 中已被弃用。提前致谢
  • 亲爱的吉普萨!我试图实现这个代码(第一种方法)。但由于我没有处理 ASIHTTPRequest,我无法理解哪种类型应该有 networkQueue。 [networkQueue addOperation: request]; [网络队列去];你能否澄清一下哪一部分给我带来了回应。应该是mapAddressResponse:方法吗???
  • 我找到了简单的方法来做 CLLocation *aLoc=[[CLLocation alloc] initWithLatitude:latitudeCL longitude:longitudeCL];
猜你喜欢
  • 2017-10-06
  • 2021-10-23
  • 2018-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-12
  • 1970-01-01
相关资源
最近更新 更多