iOS中两个位置之间的估计时间 [英] Estimated Time Between two Locations in iOS

查看:148
本文介绍了iOS中两个位置之间的估计时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



请参阅下图以获得更好的理解。 b



我看到这个什么是从任何位置到当前位置获取ETA(预计到达时间)的正确方法,以及计算在Apple地图中覆盖旅程所花费的时间但他们没有使用此解决我的问题。我总是使用这个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];

[方向calculateETAWithCompletionHandler:^(MKETAResponse * response,NSError * error){
if(error){
NSLog(@Error%@,error.description);
} else {
NSLog(@expectedTravelTime%f,response.expectedTravelTime);
}
}];
//我不知道为什么我得到Null响应到calculateETAWithCompletionHandler这个方法..

//也试过以下......
[directions calculateDirectionsWithCompletionHandler:^ (MKDirectionsResponse *响应,NSError *错误){
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]);
}
}];
//我不确定为什么我将Null响应转换为calculateDirectionsWithCompletionHandler这个方法..

我还看到了这个是否有任何方法可以确定使用Apple Maps API的两个位置之间的驾车时间?,并且如何计算在两个位置之间完成路径所需的时间?。但RightNow,我不想使用 Google Maps Directions API 。因为我的客户不想使用它。如果Google API是免费的,那么肯定我更喜欢。

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



有没有其他解决方案可以做到这一点?? ??



任何帮助都不胜感激。

解决方案

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

  NSString * strUrl = [NSString stringWithFormat:@http:/ / latitude 驾驶]; 
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]]);
}
}
其他{
NSLog(@N.A。);
}


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.

解决方案

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

这篇关于iOS中两个位置之间的估计时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆