目前,我有一个地方字典,其中包含典型地址的所有不同部分的值,然后我通过地理编码器传递。它看起来像这样:
[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];
}