计算到特定坐标的行驶距离 [英] Calculating driving distance to a particular coordinate

查看:66
本文介绍了计算到特定坐标的行驶距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以让我获得到特定 geo-cordinate 的行驶距离?我知道我必须使用 http://maps.googleapis.com/maps/api/directions/output?parameters 并获得 JSON 输出才能获得结果.

Is there a way where i could get the driving distance to a particular geo-cordinate ? I am aware that i have to use http://maps.googleapis.com/maps/api/directions/output?parameters and get a JSON output to get the results.

是否有说明如何完成此操作的教程?或者我开始的任何示例代码?

Is there a tutorial that explains how to get this done? or any sample code where i start on?

推荐答案

您可以尝试以下操作.此代码获取 Google 路线服务返回的所有路线(甚至是备用路线).你可以在第2步中得到距离(解析响应)

You may try the following. This code fetches all directions (even the alternate ones) returned by Google Directions Services. You can get the distance in step 2 (parsing response)

第 1 步:输入起点和终点位置

Step 1: Enter the From and To Locations

NSMutableString *googleURL = [[NSMutableString alloc] initWithString:[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&sensor=true&alternatives=true", searchFrom.text, searchTo.text]];
NSURL *url = [NSURL URLWithString:googleURL];
[googleURL release];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];        
[NSURLConnection connectionWithRequest:request delegate:self];
request = nil;
responseData = [[NSMutableData alloc] init];

第 2 步:当您得到查询的响应时,解析结果

Step 2: When you get the response of the query made, parse the result

- (id)parseResponseWithDictionary:(NSDictionary *)dictResponse {
    if ([[dictResponse objectForKey:@"status"] isEqualToString:kGoogleDirectionsStatusOK]) {
        NSArray *listRoutes = (NSArray *)[dictResponse objectForKey:@"routes"];
        NSMutableString *result = nil;
        NSMutableArray *listPolylines = nil;
        for (NSDictionary *dictRouteValues in listRoutes) {
            NSDictionary *dictPolyline = [dictRouteValues objectForKey:@"overview_polyline"];
            if (!result) {
                result = [[NSMutableString alloc] init];
            }
            [result appendString:[dictPolyline objectForKey:@"points"]];
            if (!listPolylines) {
                listPolylines = [[NSMutableArray alloc] init];
            }
            [listPolylines addObject:result];
            [result release];
            result = nil;
        }   
        return [listPolylines autorelease];
    }
    else {
        NSString *error = @"No result found. Please check start and end location again!";
        return error;
    }
    return nil;
}

步骤 3:解码折线列表中的每条折线.这将给出路径坐标.

Step 3: Decode each polyline in the polyline list. This will give the path coordinates.

-(NSMutableArray *)decodePolyLine:(NSMutableString *)encoded {
    [encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"
                                options:NSLiteralSearch
                                  range:NSMakeRange(0, [encoded length])];
    NSInteger len = [encoded length];
    NSInteger index = 0;
    NSMutableArray *listCoordinates = [[[NSMutableArray alloc] init] autorelease];
    NSInteger lat=0;
    NSInteger lng=0;
    while (index < len) {
        NSInteger b;
        NSInteger shift = 0;
        NSInteger result = 0;
        do {
            b = [encoded characterAtIndex:index++] - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
        lat += dlat;
        shift = 0;
        result = 0;
        do {
            b = [encoded characterAtIndex:index++] - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
        lng += dlng;
        NSNumber *latitude = [[NSNumber alloc] initWithFloat:lat * 1e-5];
        NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5];
        //CLLocation *loc = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];
        [listCoordinates addObject:[NSString stringWithFormat:@"%f,%f", [latitude floatValue], [longitude floatValue]]];
        //[loc release];
        [latitude release];
        [longitude release];
    }

    return listCoordinates;
}

这篇关于计算到特定坐标的行驶距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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