我正在构建一个支持 rails 的 iphone 应用程序,它使用 AFNetworking 在特定位置创建帖子。所以 post 模型有 lat/lng 参数,应该用客户端的当前位置填充。此时,可以发布帖子,但 lat/lng 显示为空。在我的 (save:) 方法中,我传递了一个条件来查看是否找到了一个位置——这就是失败的原因,即“没有位置”被记录下来。
- (void)save:(id)sender {
[self getLocation];
NSArray *locations;
CLLocation *location = [locations objectAtIndex:0];
Post *post = [[Post alloc] init];
post.content = self.contentTextView.text;
post.photoData = UIImagePNGRepresentation(self.imageView.image);
[self.view endEditing:YES];
ProgressView *progressView = [ProgressView presentInWindow:self.view.window];
if (location) {
[post savePostAtLocation:location withBlock:^(CGFloat progress) {
[progressView setProgress:progress];
} completion:^(BOOL success, NSError *error) {
[progressView dismiss];
if (success) {
[self.navigationController popViewControllerAnimated:YES];
} else {
NSLog(@"ERROR: %@", error);
}
}];
} else {
NSLog(@"No Location");
}
}
我也尝试过像这样实现 locationManager
-(void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
[self getLocation];
}
-(CLLocation *) getLocation{
CLLocationManager * locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
self.locationManager.distanceFilter = 80.0f;
[locationManager startUpdatingLocation];
CLLocation * location = [locationManager location];
return location;
}
我认为理想情况下我会在 CLLocationManagerDelegate 中实现 savePostAtlocation ,在那里我可以像这样传递位置数组:
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations objectAtIndex:0 /* firstObject */];
if (location) {
[Post createPostAtLocation:location...
但我想在 onSave 上创建帖子,所以我试图确定位置但遇到了一些问题。如何正确获取当前位置并将其传递到字典中?对此的任何建议将不胜感激。谢谢!