我有视图控制器,提示用户输入一些位置信息,然后单击提交。发生这种情况时,数据将被放入位置字典,然后通过方法updatePlaceDictionary
和geocode
. [userListing saveInBackground]
然后将对象发送到在线数据库。这是submit
当用户填写信息并点击提交时调用的方法,以及updatePlaceDictionary
和geocode
方法:
- (void)submit{
PFObject* userListing = [PFObject objectWithClassName:@"userListing"];
[self updatePlaceDictionary];
[self geocode];
[userListing setObject:listingLocation forKey:@"location"];
[userListing saveInBackground];
[listings addObject:userListing];
[self.navigationController popViewControllerAnimated:YES];
}
- (void)updatePlaceDictionary {
[self.placeDictionary setValue:self.streetField.text forKey:@"Street"];
[self.placeDictionary setValue:self.cityField.text forKey:@"City"];
[self.placeDictionary setValue:self.stateField.text forKey:@"State"];
[self.placeDictionary setValue:self.zipField.text forKey:@"ZIP"];
}
- (void)geocode{
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;
listingLocation = [PFGeoPoint geoPointWithLatitude:coordinate.latitude longitude:coordinate.longitude];
} else {
NSLog(@"error");
}
}];
}
这三种方法都工作得很好。问题是,在submit
方法中,这一行:
[userListing setObject:listingLocation forKey@"location"];
只是最终给键“位置”一个值(0,0)。发生这种情况是因为地理编码异步运行,并且在到达上述行时尚未完成。地理编码完成运行后如何设置此值?