【问题标题】:Try to write location in a text file尝试在文本文件中写入位置
【发布时间】:2012-03-14 12:18:18
【问题描述】:

我正在为越狱 iPhone 开发。我正在阅读 iOS 5 入门书籍。目前,我使用了书中的位置代码并稍作更改。我在代码中所做的更改是我将位置写入文本文件中。但是程序崩溃了。虽然没有任何修改的相同程序运行良好。

代码如下:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = 2.0f;

    [locationManager startUpdatingLocation];
}






- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    FILE *file = fopen("/usr/locations.txt", "a");

    if (!file)
    {
        fprintf(file, "Error opening file");
    }

    if (startingPoint == nil)
        self.startingPoint = newLocation;

    NSString *latitudeString = [NSString stringWithFormat:@"%g\u00B0",
                                newLocation.coordinate.latitude];
    latitudeLabel.text = latitudeString;


    NSNumber *latitude = [[NSNumber alloc] initWithDouble:newLocation.coordinate.latitude];
    NSNumber *longitude = [[NSNumber alloc] initWithDouble:newLocation.coordinate.longitude];


    NSString *longitudeString = [NSString stringWithFormat:@"%g\u00B0",
                                 newLocation.coordinate.longitude];
    longitudeLabel.text = longitudeString;


    NSString *horizontalAccuracyString = [NSString stringWithFormat:@"%gm",
                                          newLocation.horizontalAccuracy];
    horizontalAccuracyLabel.text = horizontalAccuracyString;


    NSString *altitudeString = [NSString stringWithFormat:@"%gm",
                                newLocation.altitude];
    altitudeLabel.text = altitudeString;

    NSString *verticalAccuracyString = [NSString stringWithFormat:@"%gm",
                                        newLocation.verticalAccuracy];
    verticalAccuracyLabel.text = verticalAccuracyString;


    CLLocationDistance distance = [newLocation
                                   distanceFromLocation:startingPoint];
    NSString *distanceString = [NSString stringWithFormat:@"%gm", distance];
    distanceTraveledLabel.text = distanceString;


    struct tm *local;
    time_t t;

    t = time(NULL);
    local = localtime(&t);

    fprintf(file, "Local time and Date: %s ", asctime(local));

    //----------------- ------------------------------------------------------------

    fprintf(file, "Latitude = %lf, Longitude = %lf \n", 
            newLocation.coordinate.latitude, newLocation.coordinate.longitude);


    fclose(file);

}

【问题讨论】:

  • 你读过Apple’s documentation关于文件系统访问的内容吗?阅读。
  • 现在,我只想在控制台上打印位置。但我也无法这样做。我的开发IDE是Xcode 4.2,iOS版本是5.0.1

标签: iphone cllocationmanager


【解决方案1】:

您最近对该问题的评论说您想打印到控制台。为了在控制台上打印,您使用NSLog()。当您对对象 %@ 使用格式说明符时,替换该对象的字符串是该对象的 description 方法。

要在控制台上打印位置,请使用

NSLog(@"New location: %@", newLocation);
NSLog(@"Old location: %@", oldLocation);

CLLocation 上的描述方法返回“'latitude, longitude +/- accuracy m (speed kph / heading) @ date-time' 形式的字符串。”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-30
    • 2020-07-16
    • 2014-01-25
    • 1970-01-01
    相关资源
    最近更新 更多