iOS中两个位置之间的估计时间

iOS中两个位置之间的估计时间,ios,objective-c,google-maps,durandal,directions,Ios,Objective C,Google Maps,Durandal,Directions,从任何地点到MyCurrentLocation获取ETA(预计到达时间)的正确方法是什么 我想,当使用点击任何批注时,我必须根据该批注和用户的当前位置设置页脚上的详细信息。 请参阅下图,以便更好地理解 我看到了这个和这个,但是他们没有用这个解决我的问题。我总是得到空响应使用这个。我把代码放在下面:- double latitude = -0.075410; double longitude = 51.512520; MKPlacemark *placemark = [[MKPlacemark

从任何地点到MyCurrentLocation获取ETA(预计到达时间)的正确方法是什么

我想,当使用点击任何批注时,我必须根据该批注和用户的当前位置设置页脚上的详细信息。

请参阅下图,以便更好地理解

我看到了这个和这个,但是他们没有用这个解决我的问题。我总是得到空响应使用这个。我把代码放在下面:-

double latitude = -0.075410;
double longitude = 51.512520;
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude, longitude) addressDictionary:nil] ;
MKMapItem *mapItemSource = [[MKMapItem alloc] initWithPlacemark:placemark];

double latitude1 = -0.132128;
double longitude1 = 51.464138;
MKPlacemark *placemark1 = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude1, longitude1) addressDictionary:nil] ;
MKMapItem *mapItemDestination = [[MKMapItem alloc] initWithPlacemark:placemark1];

MKDirectionsRequest *directionsRequest = [[MKDirectionsRequest alloc] init];
[directionsRequest setSource:mapItemSource];
[directionsRequest setDestination:mapItemDestination];
directionsRequest.transportType = MKDirectionsTransportTypeAutomobile;
MKDirections *directions = [[MKDirections alloc] initWithRequest:directionsRequest];

[directions calculateETAWithCompletionHandler:^(MKETAResponse *response, NSError *error) {
    if (error) {
        NSLog(@"Error %@", error.description);
    } else {
        NSLog(@"expectedTravelTime %f", response.expectedTravelTime);
    }
}];
//I am not sure why I am getting Null response into calculateETAWithCompletionHandler this method..

//Also tried following......
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
    if (error) {
        NSLog(@"Error %@", error.description);
    } else {
        MKRoute * routeDetails = response.routes.lastObject;
        //[self.mapView addOverlay:routeDetails.polyline];
        NSLog(@"Street %@",[placemark.addressDictionary objectForKey:@"Street"]);
        NSLog(@"distance %@",[NSString stringWithFormat:@"%0.1f Miles", routeDetails.distance/1609.344]);
        NSLog(@"expectedTravelTime %@",[NSString stringWithFormat:@"%0.1f minutes",routeDetails.expectedTravelTime/60]);
    }
}];
//I am not sure why I am getting Null response into calculateDirectionsWithCompletionHandler this method..
我也看到了这个和这个。但现在,我不想使用。因为我的客户不想用这个。如果谷歌API是免费的,那么我肯定更喜欢它


另外,我尝试了
distanceFromLocation
方法。但是,我认为,这个解决方案假设两点之间的直线距离,这几乎没有用处

有没有其他解决方案来解决这个问题


如果有任何帮助,我们将不胜感激。

您可以使用谷歌api来实现这一点,默认情况下还有一件事是有方法的

locA = [[CLLocation alloc] initWithLatitude:[[latsArray objectAtIndex:0] floatValue] longitude:[[longArray objectAtIndex:0]  floatValue]];
locB = [[CLLocation alloc] initWithLatitude:[[latsArray objectAtIndex:1] floatValue] longitude:[[longArray objectAtIndex:1]  floatValue]];

distance = [locA distanceFromLocation:locB] * 0.000621371;
散步的时间到了

 float time  = distance / 3.1; //divide by 3.1 for by walk 

试试这个方法。在这里,您必须传递源和目的地的纬度和经度信息

NSString *strUrl = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=false&mode=%@", lat,  longi, userLat,  userLong, @"DRIVING"];
NSURL *url = [NSURL URLWithString:[strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *jsonData = [NSData dataWithContentsOfURL:url];
if(jsonData != nil)
{
    NSError *error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    NSMutableArray *arrDistance=[result objectForKey:@"routes"];
    if ([arrDistance count]==0) {
        NSLog(@"N.A.");
    }
    else{
        NSMutableArray *arrLeg=[[arrDistance objectAtIndex:0]objectForKey:@"legs"];
        NSMutableDictionary *dictleg=[arrLeg objectAtIndex:0];
        NSLog(@"%@",[NSString stringWithFormat:@"Estimated Time %@",[[dictleg   objectForKey:@"duration"] objectForKey:@"text"]]);
    }
}
else{
    NSLog(@"N.A.");
}

如果要计算点与点之间的行程时间,首先需要指定驾驶方向或步行方向,然后生成方向。方向请求的结果将包括行程时间估计


您需要使用MapKit、
MKDirectionsRequest
MKDirections
类等。你还需要配置你的应用程序,向苹果公司申请生成驾驶方向的许可。我在网上找到了一个解释如何计算驾驶方向的链接。

你是否假设你的用户已经启用了
noclip
作弊代码?如果是这样的话,那么计算就是两个地点之间的直线距离除以它们的平均步行距离。否则,您需要地图数据。@JonathonReinhart,LOL。您能告诉我如何启用该欺骗吗?这可能非常有用。这个问题太广泛了。你发布了几个链接,说“但是他们没有用这个解决我的问题。”(原文如此)。具体来说,他们是如何无法满足你的需求的?@Duncan:我尝试了很多次这个答案,但总是无效。我提到了链接,因为我想展示我以前尝试过的东西。否则,您告诉我,这个问题与这些问题是重复的。请阅读它返回到分钟内的文档。。??还有驾驶、骑自行车和公交呢。它以秒为单位返回,而对于汽车来说,浮动时间=距离/43.50;如果(时间)我想,我们得到的时间是小时而不是秒。这个解决方案假设两点之间的直线距离,这几乎从来没有用过。听起来不错!谢谢@kb920。但我不知道,谷歌地图方向API怎么能在没有键的情况下给出响应。它是完全免费的。@MeetDoshi谷歌提供了一些免费的请求(我想每天有2000个请求)在那之后给error@chiragshah:它有1000个请求。但是我们必须为这1000个免费请求生成一个密钥。否则谷歌不会计算我们的请求数。但是我仍然不知道没有密钥它是如何工作的。如果没有密钥它对我来说很好。@MeetDoshi我想你不需要密钥。我使用免费url已经很久了time@MeetDos大家好,对于这个API,我们不需要任何密钥。所以我们可以不受任何限制地使用它!我没想到您会这么做。感谢@Duncac为您所做的努力。我非常感谢。