<分区>
从 AnyLocation 到 MyCurrentLocation 获取 ETA(预计到达时间)的正确方法是什么?
我想,当使用点击任何注释时,我必须根据该注释和用户的当前位置在页脚上设置详细信息。
请看下图以便更好地理解。
我看到了这个What is proper way to get ETA (estimated time arrival) from any location to my current location还有这个calculate 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? .但是现在,我不想使用 Google Maps Directions API .因为我的客户不想使用它。如果 Google API 是免费的,那么我当然更喜欢它。
此外,我尝试了 distanceFromLocation
方法。但是,我认为,此解决方案假设两点之间的直线距离几乎没有用。
是否还有其他解决方案...?
如有任何帮助,我们将不胜感激。
最佳答案
试试这个方法。这里需要传递源和目的地的经纬度信息。
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.");
}
关于ios - iOS 中两个位置之间的估计时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35625950/