1

我正在尝试在我的应用程序中计算 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)

但在谷歌地图上,我得到了超过 30 分钟的预期旅行时间和超过 9 英里的距离

我哪里错了?

4

1 回答 1

1

问题是这些转换使用intValue而不是doubleValue

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

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


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


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


intValue将上述行 更改为doubleValue.

于 2014-09-06T12:09:13.993 回答