0

目前,我有一个地方字典,其中包含典型地址的所有不同部分的值,然后我通过地理编码器传递。它看起来像这样:

[self.placeDictionary setValue:@"166 Bovet Rd" forKey:@"Street"];
[self.placeDictionary setValue:@"San Mateo"  forKey:@"City"];
[self.placeDictionary setValue:@"CA" forKey:@"State"];
[self.placeDictionary setValue:@"94402" forKey:@"ZIP"];

CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressDictionary:self.placeDictionary completionHandler:^(NSArray *placemarks, NSError *error) {
    if([placemarks count]) {
        CLPlacemark *placemark = [placemarks objectAtIndex:0];
        CLLocation *location = placemark.location;
        CLLocationCoordinate2D coordinate = location.coordinate;
        PFGeoPoint* userLocation = [PFGeoPoint geoPointWithLatitude:coordinate.latitude longitude:coordinate.longitude];
        NSLog(@"%f,%f", userLocation.latitude, userLocation.longitude); 
    } else {
        NSLog(@"location error");
        return;
    }
}];

我可以将它们合并为一个字符串以通过地理编码器,而不是为地址的每个单独部分设置一个单独的字典条目吗?这种效果的东西:

[self.placeDictionary setValue:@"166 Bovet Rd San Mateo CA 94402" forKey:@"Address"]; 

我想这样做的原因是我有一个搜索栏,用户应该在其中输入一个位置进行地理编码,我不能将它分割并提取地址的每个单独部分,所以有没有办法让我可以像这样传递整个地址进行地理编码吗?

编辑:我尝试了以下代码,终端打印“位置错误”:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [self.placeDictionary setValue:@"166 Bovet Rd San Mateo CA 94402" forKey:@"Address"]; 

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressDictionary:self.placeDictionary completionHandler:^(NSArray *placemarks, NSError *error) {
        if([placemarks count]) {
            CLPlacemark *placemark = [placemarks objectAtIndex:0];
            CLLocation *location = placemark.location;
            CLLocationCoordinate2D coordinate = location.coordinate;
            PFGeoPoint* userLocation = [PFGeoPoint geoPointWithLatitude:coordinate.latitude longitude:coordinate.longitude];
            NSLog(@"%f,%f", userLocation.latitude, userLocation.longitude); 
        } else {
            NSLog(@"location error");
            return;
        }
    }];

    [self.view endEditing:YES]; 
}
4

2 回答 2

0

在为 Street、City、State 和 Zip 键获取四个值后,您可以手动创建一个附加键并将完整地址存储在其中,否则您可以将所有这四个值存储在四个不同的字符串中,然后将它们一起保存为字典中的任何键。

于 2013-09-10T06:14:44.033 回答
0

我想到了。我只是简单地使用了该方法geocodeAddressString

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    NSString* address = [NSString stringWithFormat:@"166 Bovet Road San Mateo CA 94402"];

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
        if([placemarks count]) {
            CLPlacemark *placemark = [placemarks objectAtIndex:0];
            CLLocation *location = placemark.location;
            CLLocationCoordinate2D coordinate = location.coordinate;
            PFGeoPoint* userLocation = [PFGeoPoint geoPointWithLatitude:coordinate.latitude longitude:coordinate.longitude];
            NSLog(@"%f,%f", userLocation.latitude, userLocation.longitude); 
        } else {
            NSLog(@"location error");
            return;
        }
    }];

    [self.view endEditing:YES]; 
}
于 2013-09-10T06:20:45.120 回答