0

这是我迄今为止对我的地址进行反向地理编码的代码:

-(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];

现在我正在尝试从我的字典中对所有这些地址进行反向地理编码。有任何想法吗?

4

1 回答 1

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];
          }
    }
于 2013-11-07T20:03:15.107 回答