确定行进的确切距离

时间:2013-10-21 21:09:59

标签: core-location cllocationmanager cllocation

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

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


}

1 个答案:

答案 0 :(得分: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
}