【问题标题】:Store iPhone Location as NSString?将 iPhone 位置存储为 NSString?
【发布时间】:2013-05-10 06:59:06
【问题描述】:

我对@9​​87654321@ 还很陌生,目前正在构建我的第一个应用程序。我正在尝试将 iPhone 的位置输出到要在 JSON 请求中使用的字符串。

我已经构建了请求,但我不确定如何获取 iPhone 的位置,更不用说输入字符串和我发现难以遵循的苹果文档了。

我该怎么做?

编辑:我已经看到如何像这样实现位置:

- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation {
CLLocationDegrees latitude = newLocation.coordinate.latitude;
CLLocationDegrees longitude = newLocation.coordinate.longitude;
}

但是我不确定该放在哪里,这不是一个对象吗?

不需要初始化?

我尝试编辑它以将 newLocation 作为搅拌而不是 void 返回,但我该如何称呼它?

我需要什么输入 (CLLocationManager *)?

【问题讨论】:

    标签: iphone objective-c location


    【解决方案1】:

    CLLocationManager 类允许您跟踪当前位置。

    如果你想用它来找到你的位置,那就是你需要做的:

    // Create an instance of CLLocationManager class :
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    
    // Set the delegate of your instance :
    locationManager.delegate = self; // Set your controller as a <CLLocationManagerDelegate>.
    
    // Now update your location :
    [locationManager startUpdatingLocation];
    

    现在,每次CLLocationManager 实例更新时,都会调用委托方法:

    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
    

    这意味着每次您的locationManager 更新其位置时,都会调用此方法。

    现在您可以通过添加 .m 文件来覆盖该方法:

    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
        // Do whatever you want here
    }
    

    例如,如果您想存储当前坐标,您可以这样做:

    在 .h 中声明:

    float latitude;
    float longitude;
    

    然后完成委托方法(将这个写在.m中):

    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
        latitude = [manager location].coordinate.latitude;
        longitude = [manager location].coordinate.longitude;
    }
    

    现在您可以将值存储在 NSString 中,就像 @amar 回答的那样。

    我已经编辑了这个答案 6 次,所以我希望这能回答你的问题并帮助你:D。

    【讨论】:

      【解决方案2】:

      您只需要[NSString stringWithFormat:@"%f",&lt;your float&gt;];

      【讨论】:

      • 已编辑问题以包含有关问题的更多详细信息
      • ...你只需要[NSString stringWithFormat:@"%f",&lt;your float&gt;];,你只需要[NSString stringWithFormat:@"%f",&lt;your float&gt;];[NSString stringWithFormat:@"%f",&lt;your float&gt;];[NSString stringWithFormat:@"%f",&lt;your float&gt;];is all you need.
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 2016-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多