如何反转地理编码地址字典?

时间:2013-11-07 18:18:50

标签: ios objective-c nsarray nsdictionary geocoding

这是我到目前为止对我的地址进行反向地理编码的代码:

-(void)getJobs:(NSData *)responseData{


    NSError* error;
    NSDictionary* json = [NSJSONSerialization
                          JSONObjectWithData:responseData

                          options:kNilOptions
                          error:&error];
    NSArray *array1 = [json valueForKey:@"activities"];
    NSArray *array2 = [array1 valueForKey:@"activity_where"];
    NSArray *array3 = [array1 valueForKey:@"activity_city"];

    NSMutableArray * addresses = [NSMutableArray array];
    for(int i = 0; i < [array2 count]; i++)
    {
        NSString * geocodeAddresses = [NSString stringWithFormat:@"%@, %@",
                                        [array2 objectAtIndex:i],
                                        [array3 objectAtIndex:i]];
        [addresses addObject:geocodeAddresses];
    }

    NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
    [addressDictionary setObject:addresses forKey:@"array"];

    NSLog(@"%@",addressDictionary);

每当我记录下来时,我都会得到以下信息:

   array =     (
        "some address",
        "some address",
        "some address",
      );
}

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressDictionary:[addressDictionary valueForKey@"array"] completionHandler:^(NSArray* placemarks, NSError* error){
        for (CLPlacemark* aPlacemark in placemarks)
        {

            // Process the placemark.
            NSString *latDest = [NSString stringWithFormat:@"%.4f",aPlacemark.location.coordinate.latitude];
            NSString *lngDest = [NSString stringWithFormat:@"%.4f",aPlacemark.location.coordinate.longitude];

            CLLocationCoordinate2D annotationCoordinate =
            CLLocationCoordinate2DMake([latDest doubleValue],
                                       [lngDest doubleValue]);
            UserAnnotation *annotation = [[UserAnnotation alloc] init];
            [self.mapView addAnnotation:annotation];

现在我正试图从我的字典中反转地理编码所有这些地址。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

以上几个问题:

1)您试图反转地址字符串的地理编码,而不是地址信息的字典。你应该使用

    geocodeAddressString:completionHandler:

2)将地址字符串放入数组后,需要遍历每个字符串并在其上调用上述方法。也不需要将它们添加到地址字典中。获取可变“地址”数组并执行以下操作

   for (NSString *address in addresses)
      {
         CLGeocoder *geocoder = [[CLGeocoder alloc] init];
         [geocoder geocodeAddressString:address completionHandler:^(NSArray* placemarks, NSError* error){
         for (CLPlacemark* aPlacemark in placemarks)
         {

          // Process the placemark.
             NSString *latDest = [NSString stringWithFormat:@"%.4f",aPlacemark.location.coordinate.latitude];
             NSString *lngDest = [NSString stringWithFormat:@"%.4f",aPlacemark.location.coordinate.longitude];

             CLLocationCoordinate2D annotationCoordinate =
             CLLocationCoordinate2DMake([latDest doubleValue],
                                   [lngDest doubleValue]);
             UserAnnotation *annotation = [[UserAnnotation alloc] init];
             [self.mapView addAnnotation:annotation];
          }
    }