Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the number of routes between two places

I need to find out the number of routes from a source to a destination using the Google maps API, and then find, among those, which one is the shortest route.

I am able to get one route by using this code

-(NSArray*) calculateRoutesFrom:(CLLocationCoordinate2D) f to: (CLLocationCoordinate2D) t {
    NSString* saddr = [NSString stringWithFormat:@"%f,%f", f.latitude, f.longitude];
    NSString* daddr = [NSString stringWithFormat:@"%f,%f", t.latitude, t.longitude];

    NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.google.com/maps?output=dragdir&saddr=%@&daddr=%@", saddr, daddr];
    NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];
    NSLog(@"api url: %@", apiUrl);
    NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl];
    NSString* encodedPoints = [apiResponse stringByMatching:@"points:\\\"([^\\\"]*)\\\"" capture:1L];

    return [self decodePolyLine:[encodedPoints mutableCopy]];
}

but I'm unable to get multiple routes.

I'm new to using the Google Maps API; I followed this tutorial.

How can I do this? Can any one please post some sample code or a tutorial?

like image 250
MaheshBabu Avatar asked May 23 '11 09:05

MaheshBabu


1 Answers

alternatives (optional), if set to true, specifies that the Directions service may provide more than one route alternative in the response. Note that providing route alternatives may increase the response time from the server.

From The Google Directions API

You need to add in your query link alternatives=true

like image 180
Alex Terente Avatar answered Sep 23 '22 16:09

Alex Terente