0

我正在尝试使用 Core Location 框架计算从开始的距离,但是当我将应用程序放在 iPhone 设备上时,数据不正确。距离不断波动并显示随机数据。请帮帮我。此外,高度显示为零。

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{

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

    NSString *currentAltitude = [[NSString alloc] initWithFormat:@"%g",                                                          
                                 newLocation.altitude];

    heightMesurement.text=currentAltitude;
    [currentAltitude release];

    //Distance

    if(startingPoint==nil)
        self.startingPoint=newLocation;
    //if(newLocation.horizontalAccuracy <= 100.0f) { 
    //    [locationManager stopUpdatingLocation]; 
    //}

    //test start.......................................................

    //startlocation
    NSString *sp = [[NSString alloc] 
                            initWithFormat:@"%f",startingPoint];
    NSLog(@"\nStarting point=%@",startingPoint);
    ssp.text=sp;

    //endlocation

    NSString *ep = [[NSString alloc] 
                    initWithFormat:@"%f",newLocation]; 
    NSLog(@"\nStarting point=%@",newLocation);
    eep.text=ep;
    //test end............................................................


    CLLocationDistance mydistance=[newLocation distanceFromLocation:startingPoint];

    NSString *tripString = [[NSString alloc] 
                            initWithFormat:@"%f",mydistance]; 
    distLabel.text=tripString;

    [tripString release];
    //test........................

    [sp release];
    [ep release];

}//Location Manager ends..


//Time interval of 3 sec....
-(void)locationUpdate:(NSTimer*)timer{

    if(timer != nil) [timer invalidate];

    [self.locationManager startUpdatingLocation];
}
4

1 回答 1

1

至于为什么您的高度可能为零,请参阅this answer to a similar question

这可能只是您的NSLog陈述的问题,但是起点和终点都打印出带有以下内容的NSLog陈述

NSLog(@"\nStarting point=%@",

您似乎安排了一个 3 秒计时器的方式并不是 iOS 真正希望您使用的方式CLLocationManager。首选方法是告诉CLLocationManager您的位置标准是什么,然后开始更新。您实际上不需要一直告诉它每 3 秒开始更新一次。您可以只执行一次,然后如果您决定不再需要更新,请致电

[locationManager stopUpdatingLocation];

如果操作系统没有新的位置信息,那么继续询问可能没有意义。当它有新的位置信息时,它会通过 告诉你locationManager:didUpdateToLocation:fromLocation。所以,我建议更像这样开始这个过程:

    CLLocationManager* locationManager = [[CLLocationManager alloc] init];
    if ([locationManager locationServicesEnabled]) {
        Reachability* netStatus = [Reachability sharedReachability];
        if (([netStatus internetConnectionStatus] != NotReachable) || ([netStatus localWiFiConnectionStatus] != NotReachable)) {
            locationManager.delegate = self;
            locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
            locationManager.distanceFilter = 100.0;  // 100 m, or whatever you want
            [locationManager startUpdatingLocation];
        } else {
            // TODO: error handling
        }
    }
    self.locMgr = locationManager;
    [locationManager release];

位置管理器通常会为您提供多个结果,随着它在您的位置上不断增加准确性。如果你不断地重新启动它,我想知道这是否会导致它出现问题。

于 2012-05-16T06:48:18.863 回答