尝试在文本文件中写入位置

时间:2012-03-14 12:18:19

标签: iphone cllocationmanager

我正在为越狱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);

}

1 个答案:

答案 0 :(得分:0)

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

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

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

CLLocation上的描述方法返回“形式为'纬度,经度+/-精度m(速度kph /航向)@日期时间'的字符串。”