我正在构建一个 Rails 支持的 iPhone 应用程序,它使用 AFNetworking 在特定位置创建帖子。因此,发布模型具有纬度/经度参数,应填写客户端的当前位置。 此时,可以发布帖子,但纬度/经度显示为空。 在我的 (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...
但我想在保存时创建帖子,所以我试图确定位置,但遇到了一些问题。 如何正确获取当前位置并将其传递到字典中? 对此的任何建议将不胜感激。谢谢!
最佳答案
查看您的代码,我认为您对 CLLocationManager 的设计工作方式有一些误解。看起来您正在尝试从 locationManager:didUpdateLocations
内部调用 [self getLocation]
。这是不正确的。在按下按钮时调用的 save
方法中尝试类似的操作(我会在测试时删除当前存在的代码):
CLLocationManager * locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
self.locationManager.distanceFilter = 80.0f;
[locationManager startUpdatingLocation];
然后它将开始生成位置数据。当发生这种情况时,手机会非常快速地自动调用 locationManager:didUpdateLocations
。然后,在 locationManager:didUpdateLocations
中您可以使用:
CLLocation * location = [manager location];
NSLog(@"%@", location);
在控制台中查看您的位置数据。
我在这里写的内容应该能让手机为您生成位置数据。您在 locationManager:didUpdateLocations
中所说的 createPostAtLocation:
可能是正确的方法。获取位置数据后,调用 [manager stopUpdatingLocation] 让手机停止,然后将获取的位置数据发布回服务器。
关于iphone - 将当前位置从 iPhone 发布到 Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17765214/