1

我正在使用此代码获得地标,但它没有给出城市名称。早些时候,我使用 MKReverse Geocoder 来获取获取城市名称的地标,但在 iOS 6 中它显示已弃用,因为 Apple 开发人员在 CLLocation 中添加了所有内容。

所以我使用了这段代码:

-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{

    CLLocation *location = [locationManager location];
    NSLog(@"location is %@",location);

    CLGeocoder *fgeo = [[[CLGeocoder alloc] init] autorelease];

    // Reverse Geocode a CLLocation to a CLPlacemark
    [fgeo reverseGeocodeLocation:location
          completionHandler:^(NSArray *placemarks, NSError *error){

               // Make sure the geocoder did not produce an error
               // before continuing
               if(!error){
                    // Iterate through all of the placemarks returned
                    // and output them to the console
                    for(CLPlacemark *placemark in placemarks){
                           NSLog(@"%@",[placemark description]);
                           city1= [placemark.addressDictionary objectForKey:(NSString*) kABPersonAddressCityKey];
                           NSLog(@"city is %@",city1);
                    }
               }
               else{
                    // Our geocoder had an error, output a message
                    // to the console
                    NSLog(@"There was a reverse geocoding error\n%@",
                                         [error localizedDescription]);
               }
         }
    ];

}

正如我在控制台中看到的那样, NSLog(@"%@",[placemark description]); 它给出的输出如下:- abc 道路名称、abc 道路名称、州名、国家/地区名称。

4

2 回答 2

4

如果你想 NSLog 地址,你必须像这样编写它:

NSString *street = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressStreetKey];
NSString *city = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressCityKey];
NSString *state = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressStateKey];
NSString *country = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressCountryKey];
NSString *zip = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressZIPKey];

NSString *message = [NSString stringWithFormat:@"Address Is: %@, %@ %@, %@, %@", street, zip, city, state, country];

NSLog(@"%@", message);

或者您可以遍历返回的数组:

NSArray *array = [[placemark addressDictionary] objectForKey:@"FormattedAddressLines"];

如果您只想打印字典内容,请执行以下操作:

NSLog(@"%@", [[placemark addressDictionary] description]);
于 2012-11-05T17:04:16.437 回答
4

我可以看到的三件事可能会引起注意...

首先,不推荐使用此方法(请参阅此处的文档):

(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

尝试改用这个:

(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

您的最新位置是数组中的最后一项,locations因此:

 /* 
 According to the Apple docs, the array of locations keeps the most recent location as the
 last object in the array 
 */
CLLocation *location = [locations lastObject];
NSLog(@"%@", location);

其次,如果您使用 ARC,则不需要自动释放消息:CLGeocoder *fgeo = [[CLGeocoder alloc] init];工作正常。

最后,根据 CLPlacemark 的 Apple 文档,有一个属性locality应该返回城市作为地标。所以

for(CLPlacemark *placemark in placemarks){
    NSLog(@"%@",[placemark description]);
    NSString *city1 = [placemark locality];
    NSLog(@"city is %@",city1); }

如果您只想要城市,那么该addressDictionary物业似乎是矫枉过正的。根据此处的文档, addressDictionary 被格式化为返回 ABPerson 对象中的内容,我假设您必须对其进行解析才能获取城市。地标位置似乎要简单得多......

我在我正在构建的地理编码应用程序中测试了我的建议,我得到了你正在寻找的结果[placemark locality]

于 2012-11-05T20:05:00.213 回答