我从MKRoute那里得到了错误的预期旅行时间

时间:2014-09-06 02:48:08

标签: objective-c mapkit cllocationcoordinate2d

我正在尝试计算我的应用中的expectedTravelTime,但与GoogleMaps相比,我获得了不同的值。

这是我的代码:

NSArray *startLocationArray = [startLocation componentsSeparatedByString:@","];
NSArray *destinationArray = [destination componentsSeparatedByString:@","];

MKPlacemark *placemarkStartLocation = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake([startLocationArray[0] intValue],
                                                                                            [startLocationArray[1] intValue]) addressDictionary:nil];
MKMapItem *mapItemStartLocation = [[MKMapItem alloc] initWithPlacemark:placemarkStartLocation];

MKPlacemark *placemarkDestination = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake([destinationArray[0] intValue],
                                                                                            [destinationArray[1] intValue]) addressDictionary:nil];
MKMapItem *mapItemDestination = [[MKMapItem alloc] initWithPlacemark:placemarkDestination];

MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
[request setSource:mapItemStartLocation];
[request setDestination:mapItemDestination];
[request setTransportType:MKDirectionsTransportTypeAutomobile];
[request setRequestsAlternateRoutes:NO];
MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
__block MKRoute *route;

[directions calculateDirectionsWithCompletionHandler:
 ^(MKDirectionsResponse *response, NSError *error)
{
    if ( ! error && [response routes] > 0)
    {
        route = [[response routes] objectAtIndex:0];

        double exceedingTime = fmod(route.expectedTravelTime, 60);
        eta = (route.expectedTravelTime - exceedingTime)/60;
        int iEta = (int)eta;

        lblEta.text = [NSString stringWithFormat:@"%im", iEta, nil];
    }
}];

startLocation和destination在方法之外传递,它们的值为:

startLocation = 37.776402,-122.408470
destination = 37.831287,-122.485519

我明白了:

route.distance = 1513 (meters)
route.expectedTravelTime = 491 (should be seconds, so finally 8 minutes)

但是在GoogleMaps上,我得到的预计旅行时间超过30分钟,超过9英里 距离

我哪里错了?

1 个答案:

答案 0 :(得分:1)

问题在于这些转化使用intValue代替doubleValue

[startLocationArray[0] intValue], [startLocationArray[1] intValue]

[destinationArray[0] intValue], [destinationArray[1] intValue]


起始坐标和目标坐标具有小数部分(例如37.776402),因此您必须使用doubleValue(与CLLocationCoordinate2D结构中定义的纬度和经度类型相匹配)。


使用intValue,代码将获得从37, -12237, -122的路线,这是在加利福尼亚州圣克鲁斯的一个高尔夫球场内的短途旅行。


将上述行中的intValue更改为doubleValue