【问题标题】:Objective-C: Finding User Location (Not on a Map)Objective-C:查找用户位置(不在地图上)
【发布时间】:2015-02-23 21:34:58
【问题描述】:

我正在尝试让我的应用以类似地址的形式(位于“反向地理编码”注释之后)找到用户的位置。以下是我的代码:

- (IBAction)getuserlocation:(id) sender{
    //Getting Location
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startUpdatingLocation];
    NSLog(address);
}
#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        longitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
        latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
    }

    // Stop Location Manager
    [locationManager stopUpdatingLocation];

    // Reverse Geocoding
    NSLog(@"Resolving the Address");
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
        if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];
            address = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
                                 placemark.subThoroughfare, placemark.thoroughfare,
                                 placemark.postalCode, placemark.locality,
                                 placemark.administrativeArea,
                                 placemark.country];
        } else {
            NSLog(@"%@", error.debugDescription);
        }
    } ];

}

我的问题是编译器只打印出“(null)”。 (我执行此操作的行位于“getuserlocation”IBAction 中。)我不确定这是为什么。我已经在实际的 iPhone 6 Plus 上测试了该应用程序,与模拟器不同的是,它可以访问位置服务。

如果有人能够指出我的代码中的错误/问题在哪里,导致它打印出 null,我将不胜感激。提前感谢所有回复的人。

***顺便说一句:以下几行在我的 viewDidLoad 部分: locationManager = [[CLLocationManager alloc] init]; geocoder = [[CLGeocoder alloc] init];

【问题讨论】:

  • 仅供参考-记录任何内容的不是编译器。记录消息的是您正在运行的应用程序。
  • 好的。这就是我要打字的意思。对此可能造成的任何混乱,我们深表歉意。
  • 除了在设置值之前访问地址变量之外,您还需要确保在 info.plist 中放入了适当的原因字符串并调用了 requestWhenInUseAuthorization 。您还使用了已弃用的位置委托方法。你应该使用didUpdateLocations:。最后,在收到第一个位置后禁用位置更新并不是一个好主意,因为第一个位置可能不会很准确。您应该继续接收更新,可能会等待CLLocationhorizonalAccuracy 属性中的低值
  • @Paulw11 感谢您的快速回复!我对 Objective-C 编程相当陌生,所以我应该把它放在我的 info.plist 文件中的什么位置?我在这个文件的任何地方都没有看到原因字符串选项。另外,我如何获得我的应用请求权限以接收对用户当前位置的访问权限?

标签: ios objective-c null geolocation core-location


【解决方案1】:

您在获取位置并将其转换为地址之前很久就记录了address

NSLog(address); 移动到您实际为address 赋值的位置之后。

顺便说一句 - 它应该更像:NSLog(@"Address: %@, address);

【讨论】:

  • 好的。那么,如果我需要在我的 IBAction 中使用它,如何在将值分配给它们之前从方法中访问地址?顺便谢谢你的回复。 :)
  • 我试着像你告诉我的那样移动 NSLog,但没有任何东西登录到控制台。没有到达此代码吗?
  • 您的代码中是否收到任何其他日志消息?您的应用是否有权使用位置服务?您是否设置了您的位置管理器以请求权限?
  • 我的控制台中也没有其他日志。另外,我不确定如何请求获取位置的权限。这可能是我的问题吗?我无法找到如何在线执行此操作。
  • 我做了并添加了@paulw11 说我应该的行,但它仍然没有得到我的位置。不知道该怎么办。
猜你喜欢
  • 2012-02-27
  • 2013-11-21
  • 2017-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多