0

我有视图控制器,提示用户输入一些位置信息,然后单击提交。发生这种情况时,数据将被放入位置字典,然后通过方法updatePlaceDictionarygeocode. [userListing saveInBackground]然后将对象发送到在线数据库。这是submit当用户填写信息并点击提交时调用的方法,以及updatePlaceDictionarygeocode方法:

- (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)。发生这种情况是因为地理编码异步运行,并且在到达上述行时尚未完成。地理编码完成运行后如何设置此值?


4

1 回答 1

0

你这里的整个代码必须有点异步。您的地理编码方法可以传入一个块回调,并在您完成地理编码时调用它。

- (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:(void (^)(void))callback {
    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]; 

            if (callback) {
                callback();
            }

        } else {
            NSLog(@"error");
        }
    }];    
}
于 2013-08-12T02:23:48.800 回答