Вопрос

What is wrong here? I am trying to get total distance travelled but when I start driving back to the point I started my value goes down? What needs to be fixed. Thank you.

    -(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


}
Это было полезно?

Решение

try this approach:

-(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
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top