具体地址计算路由
问题描述:
我使用的计算距离这两个代码点(公里或英里)具体地址计算路由
-(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];
}
}
代码工作正常,但计算出的距离之间不正确,因为根据车的路线不计算example.Basically与此代码mapView只绘制一条直线,并没有绘制一个正确的目的地。任何人都可以帮助我根据路线计算距离,我可以采取哪些措施?
答
-(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]);
} .
改为使用'MKDirections'和'MKDirectionsRequest'。 – Larme
@Larme我怎么能从问题开始编码?我想看看其他的*问题,但不成功 – breadevelopment
你需要计算一个itinary。有很多关于这方面的问题。 – Larme