-2
- (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)谢谢 :)

4

2 回答 2

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;
}
于 2012-11-10T19:17:57.207 回答
-1

手动在 locationManager 的每个回调中:)

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

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

old = new
于 2012-11-10T19:21:52.343 回答