位置管理器的交换距离

时间:2012-11-10 18:57:15

标签: iphone xcode map gps distance

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

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


    CLLocationDistance distance = [newLocation
                                getDistanceFrom:startingPoint];



    NSString *distanceString = [[NSString alloc]
                                initWithFormat:@"%.2f Miles", totalDistance *0.000621371];
   DistanceTotal.text = distanceString;
}




@end

我正在为我的应用程序制作一个距离计算器。当我运行应用程序时,文本字段为空,没有距离信息。你能帮我找到问题吗? (文本域名为DistanceTotal)谢谢:)

2 个答案:

答案 0 :(得分:2)

您是否尝试计算累积距离?接近开始时,从开始到当前位置的距离应该减小。

也许你想要这个......

// in the class containing the location manager
@property (nonatomic, assign) CLLocationDistance totalDistance;

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

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


   //CLLocationDistance distance = [newLocation
   //                               getDistanceFrom:startingPoint];

   if (!oldLocation)
       totalDistance == 0.0;
   else
       totalDistance += [newLocation distanceFromLocation:oldLocation];           

   NSString *distanceString = [[NSString alloc]
                                 initWithFormat:@"%.2f Miles", totalDistance *0.000621371];
   distanceTraveledLabel.text = distanceString;
}

答案 1 :(得分:-1)

从locationManager的每个回调中手动操作:)

fullDistance = 0 ;//add it up
newDistance = GPS();//get with location
oldDistance

delta = abs(oldDist - newDist)
full += delta;

old = new