0

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.

enter image description here

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.

Community
  • 1
  • 1
Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
  • 1
    Are you assuming your user's have enabled the `noclip` cheat code? If so, then the calculation is simply the straight-line distance between the locations, divided by their average walking distance. Otherwise, you need map data. – Jonathon Reinhart Feb 25 '16 at 11:30
  • 1
    @JonathonReinhart, LOL. Can you show me how to enable that cheat? That could be very useful. – Duncan C Feb 25 '16 at 12:05
  • This question is much too broad. You posted several links and said "But they didn't solved my problem using this." (sic). How did they fail to meet your need, **specifically**? – Duncan C Feb 25 '16 at 12:08
  • @Duncan : I tried that answer many times, but always getting null. I mentioned that links, because I want to show that what I tried before. Otherwise, you told that, this question is duplicated with those questions. – Meet Doshi Feb 25 '16 at 12:12
  • You posted 4 links in your question. You get the same null result using the technique in all 4 links? How about you extract the relevant code, add it to your question, and show exactly where it seems to be failing? – Duncan C Feb 25 '16 at 13:27
  • You say you don't want to use Google Maps directions API. You will absolutely need to use some mapping service unless you assume a straight line route from the user's current location to the destination. – Duncan C Feb 25 '16 at 13:29
  • I only getting null data into 1st two link. Other two links saying for use Google Maps Directions APIs. But My client don't want to pay for this. So RightNow, I don't want to use that. If any other free solution is there, then I preffered that. If google maps APIs are free, then always I preffered that. – Meet Doshi Feb 25 '16 at 13:30
  • @Duncan : Yeahhhh, I know, at the end, I must have to use Google Maps APIs. All other solutions are not accurate comparing to that. But I don't want to pay for this. Thats why I posted this question. Because I hope, there is some other free solution. If I didn't found any other solution, then I Definitely go with Google APis – Meet Doshi Feb 25 '16 at 13:34
  • @DuncanC : If you still want to close this question, then you can do. But I would like to share that, if some of your efforts are help me to findout this solution instead of closing this question, then its great for me. its your choice dude. But still I appreciated for your efforts. Thanks DuncanC. – Meet Doshi Feb 25 '16 at 13:44
  • 1
    Please read the documentation https://developers.google.com/maps/documentation/distance-matrix/intro – Teja Nandamuri Feb 25 '16 at 14:09
  • You should edit your question to clearly state the issues. "The first 2 links are returning null
    The other 2 links suggest using the Google Maps API, but my client does not want to use that." Provide enough information on the problems you are having so that we can actually help you. Otherwise you should delete your question, as it does not provide enough information for us to be able to answer it.
    – Duncan C Feb 25 '16 at 15:05
  • @DuncanC : Thanks.. I take cake of this things in future. – Meet Doshi Feb 26 '16 at 04:16

3 Answers3

13

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.");
}
kb920
  • 3,039
  • 2
  • 33
  • 44
  • It sounds good ! thanks @kb920. But I don't know, how can Google Maps Directions APIs gives response without key. Its totally free..? – Meet Doshi Feb 25 '16 at 13:28
  • @MeetDoshi google provide some amount of request free ( I think 2000 request per day) after that gives error – Chirag Shah Feb 25 '16 at 13:38
  • @chiragshah : Its 1000 requests. But we have to generate a key for that 1000 free requests. Otherwise google don't calculate our request counts. But I am still don't know how it works without key. Its great for me, If it works without key. – Meet Doshi Feb 25 '16 at 13:42
  • 1
    @MeetDoshi I dont think you required the key. I am using free url for long time – Chirag Shah Feb 25 '16 at 14:21
  • 1
    @MeetDoshi For this API we don't need any key. So we can use it wthout any limitation! – kb920 Feb 25 '16 at 14:37
3

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.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
1

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 
Sabby
  • 403
  • 3
  • 15
  • It returns into minutes..?? And what about driving, bicycling and transit. – Meet Doshi Feb 25 '16 at 11:48
  • 1
    it returns in seconds and for cars float time = distance / 43.50; if(time <= 1) { time = time * 60; self.timeLabel.text = [NSString stringWithFormat:@"%.2f minutes", time]; } else { self.timeLabel.text = [NSString stringWithFormat:@"%.2f hours", time]; } – Sabby Feb 25 '16 at 11:51
  • I think, we got time in Hours not in Seconds. – Meet Doshi Feb 25 '16 at 12:04
  • 2
    This solution assumes straight-line distance between two points, which is almost never useful. – Duncan C Feb 25 '16 at 12:07