-2

我正在尝试使用 JSON 创建一个自动完成地址建议。我面临的问题是我可以从用户在UISearchDisplay. 但是我正在尝试将这些数据解析为地址名称。我试图使用 Apple 的反向地理编码,但没有成功。我只收到一个值。我立即使用苹果地理编码,但结果不是我所期望的。这是代码:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{

    NSError *error;

    NSString *lookUpString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=true", self.searchBar.text];

    lookUpString = [lookUpString stringByReplacingOccurrencesOfString:@" " withString:@"+"];

    NSData *jsonResponse = [NSData dataWithContentsOfURL:[NSURL URLWithString:lookUpString]];

    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonResponse options:kNilOptions error:&error];

    self.locationArray = [[[jsonDict valueForKey:@"results"] valueForKey:@"geometry"] valueForKey:@"location"];

    int total = self.locationArray.count;

    for (int i = 0; i < total - 1; i++)
    {
        NSLog(@"locationArray count: %d", self.locationArray.count);
        NSArray *localLocations;
        localLocations = [self.locationArray objectAtIndex:i];
        NSLog(@"%d",i);
        NSString *latitudeString = [localLocations valueForKey:@"lat"];
        NSString *longitudeString = [localLocations valueForKey:@"lng"];

        NSLog(@"LatitudeString:%@ & LongitudeString:%@", latitudeString, longitudeString);

        NSString *statusString = [jsonDict valueForKey:@"status"];

        NSLog(@"JSON Response Status:%@", statusString);

        double latitude = 0.0;
        double longitude = 0.0;

        latitude = [latitudeString doubleValue];
        longitude = [longitudeString doubleValue];

        CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];

        [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error){
            [[self placemarks] addObject:placemarks];
            NSLog(@"PLACEMARKS %d", self.placemarks.count);

        }];

    }
    NSLog(@"Mutable array %d", self.placemarks.count);
        [self.searchDisplayController.searchResultsTableView reloadData];
    return NO;
}

唯一显示NSLog(@"PLACEMARKS %d", self.placemarks.count);一次,在任务结束时。有什么建议么?

4

1 回答 1

0

我真的做的比必要的多得多。

这是我现在的代码:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{

    NSError *error;

    NSString *lookUpString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&components=country:AU&sensor=false", self.searchBar.text];

    lookUpString = [lookUpString stringByReplacingOccurrencesOfString:@" " withString:@"+"];

    NSData *jsonResponse = [NSData dataWithContentsOfURL:[NSURL URLWithString:lookUpString]];

    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonResponse options:kNilOptions error:&error];

    self.locationArray = [[jsonDict valueForKey:@"results"] valueForKey:@"geometry"];

    int total = self.locationArray.count;
    NSLog(@"locationArray count: %d", self.locationArray.count);

    for (int i = 0; i < total; i++)
    {
        NSString *statusString = [jsonDict valueForKey:@"status"];
        NSLog(@"JSON Response Status:%@", statusString);

        NSLog(@"Address: %@", [self.locationArray objectAtIndex:i]);

    }

    [self.searchDisplayController.searchResultsTableView reloadData];
    return NO;
}
于 2013-04-29T14:02:02.153 回答