iOS中两个地点之间的预计时间

时间:2016-02-25 11:26:22

标签: ios objective-c google-maps durandal directions

从AnyLocation到MyCurrentLocation获取ETA(预计到达时间)的正确方法是什么?

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

请参阅下面的图片以便更好地理解。

enter image description here

我看到了这个What is proper way to get ETA (estimated time arrival) from any location to my current locationcalculate time taken to cover a journey in Apple Maps但是他们并没有用这个来解决我的问题。我总是得到Null响应使用它。我把我的代码放在下面: -

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..

我也看到了这个Is there any way to determine the driving time between two locations using Apple's Maps API?How to calculate time required to complete the path in between two locations?。但是RightNow,我不想使用Google Maps Directions API。因为我的客户不想使用它。如果Google API是免费的,那么我绝对喜欢它。

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

还有其他解决办法吗???

任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:13)

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

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.");
}

答案 1 :(得分:3)

如果您想计算点之间的旅行时间,首先需要指定是否需要驾车或步行路线,然后生成路线。路线请求的结果将包括旅行时间估算。

您需要使用MapKit,MKDirectionsRequestMKDirections类等。您还需要配置您的应用以请求Apple获得生成行车路线的许可。我在网上找到了this链接,解释了如何计算行车路线。

答案 2 :(得分:1)

你可以使用google 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