Estimated Time Between two Locations in iOS [closed]

◇◆丶佛笑我妖孽 提交于 2019-12-06 15:13:40

问题


What is proper way to get ETA (estimated time arrival) from AnyLocation to MyCurrentLocation?

I want, when use tap on any annotation, then I have to set details on the Footer according to that annotation and user's current location.

Please see below image for better understanding.

I saw this What is proper way to get ETA (estimated time arrival) from any location to my current location and this calculate time taken to cover a journey in Apple Maps But they didn't solved my problem using this. I am getting always Null response using this. I put my code below:-

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

Also I saw this Is there any way to determine the driving time between two locations using Apple's Maps API? and this How to calculate time required to complete the path in between two locations?. But RightNow, I don't want to use Google Maps Directions API. Because my client does not want to use that. If Google APIs are free, then Definitely I preferred that.

Also, I tried distanceFromLocation method. But, I think, This solution assumes straight-line distance between two points, which is almost never useful.

Is there any other solution for doing this..??

Any help would be appreciated.


回答1:


Try this method. Here you have to pass source and destination latitude and longitude information.

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



回答2:


If you want to calculate the travel time between points you first need to specify if you want driving or walking directions, and then generate directions. The result of the directions request will include a travel time estimate.

You'll need to use MapKit, MKDirectionsRequest, and MKDirections classes, among others. You'll also need to configure your app to ask Apple for permission to generate driving directions. I found this link online that explains how to calculate driving directions.




回答3:


you can use google api for that and one more thing by default there is method

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;

and time for walk

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


来源:https://stackoverflow.com/questions/35625950/estimated-time-between-two-locations-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!