使用特定地址计算路线

时间:2017-07-21 16:24:40

标签: objective-c mkmapview

我使用此代码计算两点(km或mi)之间的距离

-(void) CalculateDistance {

    CLLocation *startLocation = [[CLLocation alloc] initWithLatitude:[self.address.originlatitude doubleValue] longitude:[self.address.originlongitude doubleValue]];

    CLLocation *endlocation = [[CLLocation alloc] initWithLatitude:[self.address.destinationlatitude doubleValue] longitude:[self.addresse.destinationlongitude doubleValue]];



    if (self.address.origin && self.address.origin.length > 0) {

        CLLocationDistance calc = [startLocation distanceFromLocation:endlocation];

        double totalDistance = 0.0;
        double totalMileage = 0.0;

        totalDistance = totalDistance + (calc / 1000);
        totalMileage = totalMileage + (calc / 1609);



        if (self.address.distanceKind && ![self.address.distanceKind isEqualToString:@"mi"]) {
            self.distanceTextField.text = [self.numberType stringFromNumber:[NSNumber numberWithFloat: totalDistance]];
            self.address.distance = [NSDecimalNumber decimalNumberWithString:self.distanceTextField.text];
        }else if (self.address.distanceKind &&! [self.address.distanceKind isEqualToString:@"km"]){
            self.distanceTextField.text = [self.numberType stringFromNumber:[NSNumber numberWithFloat: totalMileage]];
            self.address.distance = [NSDecimalNumber decimalNumberWithString:self.distanceTextField.text];

        }


    }

代码工作正常但计算的距离不正确,因为不是根据汽车的路线计算的例子。基本上这个代码mapView只绘制一条直线并且没有绘制正确的目的地。任何人都可以帮助我根据路线计算距离我可以采取哪些步骤?

1 个答案:

答案 0 :(得分:0)

-(void)DistanceTwoLocation 
{
  **//This is current location take code LocationOne**

 CLLocation *LocationOne = [[CLLocation alloc] initWithLatitude:Location_Manager.location.coordinate.latitude longitude:Location_Manager.location.coordinate.longitude];

**//This is current location take code LocationTwo**

 CLLocation * LocationTwo = [[CLLocation alloc] initWithLatitude:[yourLatValue] longitude:[yourLongValue];

 CLLocationDistance DistanceCalc = [LocationOne distanceFromLocation: location];

DistanceCalc = DistanceCalc/1000;

NSLog(@“%@”,[NSString stringWithFormat:@"Dist:%.2f Km",distance]);
} . 
相关问题