2014-10-04 26 views
0

我需要计算在我的计划是所有的GPS参数,所以我使用这段代码:如何在同一时间计算从CLLocationManager所有的GPS数据

(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { 
    CLLocation* loc = [locations lastObject]; 

它收集所有的GPS测量(高度,速度等等)在loc对象中,我有经纬度信息(loc.coordinate.lattitudeloc.coordinate.longitude),但这些是新测量的坐标。

由于我还想计算测量之间的距离,我如何从didUpdateLocations方法中检索“新”坐标和“旧”坐标?

如果我添加其他的LocationManager调用与程序的didUpdateToLocation方法(这将给新老坐标):

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

最初的LocationManager方法(didUpdateLocations)完全不叫,所以我不能计算所有GPS数据加上距离...

+0

您列出的第二种方法是弃用 - 取代第一种,但如果您执行它,然后第一个方法不会被调用 - 就像你找到的那样。您需要将以前的位置存储在一个属性中,并在下次调用时根据此比较/计算 – Paulw11 2014-10-04 10:21:54

+0

由于我是iOS编程的新手,我如何将位置对象复制到另一个对象(即旧位置) CLLocationManage(所以我可以比较下一个CLLocationManager方法调用的最新位置数据)?我可以访问loc对象的lat/long属性,但我将调用的CLLocationDistance方法需要对象作为参数,而不是属性。 – DaliGR 2014-10-04 16:43:05

+0

只需创建一个属性为您的类存储CLLocation – Paulw11 2014-10-04 21:48:17

回答

0

与物业副本我有这样的工作代码,但我不能让感觉成功后:

  • (void)locationManager:(CLLocationManager *)管理器didUpdateLocations:(NSArray *)locations { _locManagerIteration ++; NSLog(@“Location manager iteration =%ld”,_locManagerIteration);

    CLLocation * loc = [locations lastObject]; //位置保证至少有一个对象

    if(_locManagerIteration == 1){//初始化第一个过去的坐标对象 self.pastCoordinate = [locations lastObject]; } else { CLLocationDistance _pointDistance = [self.pastCoordinate distanceFromLocation:loc]; NSLog(@“POINT TO POINT DISTANCE =%f”,_pointDistance); self.pastCoordinate = [位置lastObject]; (@“POINT TO POINT DISTANCE =%f”,_pointDistance);

    _totalDistance = _totalDistance + _pointDistance; // COMPUTE T​​OTAL DISTANCE _totalDistance = _totalDistance/1000; //在Kilometters中翻译总距离 在上面的部分中,CLLocationDistance调用正确地将距离以米为单位放置在我的全局变量_pointDistance中。我打印_pointDistance变量并查看数字(NSLog ...)。然而,当我再次打印相同的全局变量的if-then语句值始终为零外...

内其他层夹_pointDistance变量在Xcode黑色(并打印值)并在其他支架外显示为绿色,值为零....不能从中做出任何意义...