0

这里有什么问题?我试图获得总行驶距离,但是当我开始开车回到起点时,我的价值下降了?需要修复什么。谢谢你。

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

    if (startLocation == nil)
        self.startLocation = newLocation; //if this is the first update colocation called startlocation = new location

    CLLocationDistance distanceBetween = [newLocation
                                          distanceFromLocation:startLocation]; //here is my problem I think

    NSString *tripString = [[NSString alloc]       //convert to string
                            initWithFormat:@"%f",
                            distanceBetween];
    distance.text = tripString; //update my distance label called distance


}
4

1 回答 1

1

试试这个方法:

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    if (startLocation == nil)
    {
        self.totalDistanceBetween = 0; // declare this variable
        self.startLocation = newLocation;
    }
    CLLocationDistance distanceBetween = [newLocation distanceFromLocation:startLocation ];
    startLocation = newLocation;
    self.totalDistanceBetween += distanceBetween; // declare this variable
    NSString *tripString = [[NSString alloc]       //convert to string
                            initWithFormat:@"%f",
                            self.totalDistanceBetween];
    distance.text = tripString; //update my distance label called distance
}
于 2013-10-21T21:18:51.850 回答