计算到特定坐标的行驶距离

时间:2011-10-31 17:49:49

标签: iphone objective-c xcode

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

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

2 个答案:

答案 0 :(得分:4)

您可以尝试以下方法。此代码提取Google Directions Services返回的所有路线(甚至是备用路线)。您可以在步骤2中获取距离(解析响应)

步骤1:输入发件人和收件人地点

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:当您获得所做查询的响应时,解析结果

- (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:解码折线列表中的每条折线。这将给出路径坐标。

-(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;
}

答案 1 :(得分:0)

这个使用CoreLocation和MapKit的新tutorial/sample code帮了我一堆。