CLGeocoder根据给定位置进行反向地理编码

4

如果我有一个经度和纬度(不是我的当前位置),我该如何使用GLGeocoder进行反向地理编码查找?

self.geoCoder = [[CLGeocoder alloc] init];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
//    [self.locationManager startUpdatingLocation];

[self.geoCoder reverseGeocodeLocation: locationManager.location completionHandler: ^(NSArray *placemarks, NSError *error) {
//        [self.locationManager stopUpdatingLocation];

     CLPlacemark *placemark = [placemarks objectAtIndex:0];
    // Long address
    // NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
    // Short address
    NSString *locatedAt = [placemark subLocality];

     cell.textLabel.text = spot.name;
     cell.detailTextLabel.text = [NSString stringWithFormat:@"Cab no: %@, spotted at %@", spot.cabno, locatedAt];
 }];

我需要将经度和纬度设置为反向地理编码的目标,但显然只会针对我的位置进行地理编码。

2个回答

7

我从未尝试过这个,但你不能创建自己的CLLocation对象吗?

如果您知道当前的经度和纬度,您可以 -

CLLocation *location = [[CLLocation alloc]initWithLatitude:someValue longitude:someValue];
[self.geoCoder reverseGeocodeLocation: location completionHandler: ^(NSArray *placemarks, NSError *error) {
    //do something
});

出错了!谢谢。我尝试使用initWithLatitude,但它可能是在CLLocationManager而不是CLLocation对象上。干杯! - James
太好了,你救了我。 - saintjab
我也用它来从PFGeoPoint反向地理编码,效果非常棒! - Derek Saunders

0

如果您使用模拟器,您可以调试>位置>自定义位置,然后输入经纬度以模拟任何位置。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接