steak2002
steak2002

Reputation: 177

Google Maps directions as a map on iPhone

I'm working on an iPhone app that needs to display walking directions from a point, A, to another point, B, in a UIWebView that loads a Google Maps URL with the relevant parameters.

Example of the URL i am loading:

[self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://maps.google.com/maps?saddr=55.720923,12.513428&daddr=55.745666,12.546387"]]];

I need the UIWebView to show the directions as a map (as lines on the map) as illustrated image in the link below:

How I want the directions to be displayed (directions as a map)

However, when this URL is loaded on the iPhone, the directions are instead displayed as a list (see the link below).

How the directions are displayed (directions as a list)

The user is required to press the map button in the top bar in order to see the directions as a map.

How can I set the Google Maps URL parameters such that the directions are displayed as a map directly (not as a list!) in my iPhone app?

Upvotes: 4

Views: 2547

Answers (2)

Vaibhav Limbani
Vaibhav Limbani

Reputation: 765

I'm not sure but you should refer This link Google has well explained it "Google Maps URL Scheme"

You can use

"comgooglemaps://?saddr=Google+Inc,+8th+Avenue,+New+York,+NY&daddr=John+F.+Kennedy+International+Airport,+Van+Wyck+Expressway,+Jamaica,+New+York&directionsmode=transit"

which has different parameters like saddr ,daddr ,directionsmode

Hope this helps!

Upvotes: 0

Dilip Manek
Dilip Manek

Reputation: 9143

This is lot easier if you use MKMapView.check nvpolyline project from github. it will help you to create line between point a to pont b,which solves your first problem.

And for second problem add this code in your project

NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&sensor=false", saddr, daddr];
    //&alternatives=true

    NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];

    NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl];

    SBJsonParser *jsonParser = [SBJsonParser new];
    NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:apiResponse error:nil];

    NSLog(@"apiResponse is : %@",jsonData);

    //to display route in drop down

    NSArray *routeArray = [[NSArray alloc]init];
    routeArray= [jsonData objectForKey:@"routes"];

    for(int i=0;i<[routeArray count];i++)
    {
        NSDictionary *tempDictionary = [routeArray objectAtIndex:i];
        if([tempDictionary objectForKey:@"overview_polyline"]!=nil)
        {
            NSDictionary *secTempDictionary = [tempDictionary objectForKey:@"overview_polyline"];
            if([secTempDictionary objectForKey:@"points"]!=nil)
            {
                NSString * routePoint =[secTempDictionary objectForKey:@"points"];

                [routeSetAry addObject:routePoint];

                encodedPoints = [secTempDictionary objectForKey:@"points"];
            }
            // NSLog(@"secTempDictionary is: %@", secTempDictionary);
        } 
        if([tempDictionary objectForKey:@"legs"]!=nil)
        {
            NSArray *lagArray = [[NSArray alloc]init];
            lagArray= [tempDictionary objectForKey:@"legs"];

            for(int i=0;i<[lagArray count];i++)
            {
                NSDictionary *thirdTempDictionary = [lagArray objectAtIndex:i];
                if([thirdTempDictionary objectForKey:@"steps"]!=nil)
                {
                    NSArray *stepsArray = [[NSArray alloc]init];
                    stepsArray= [thirdTempDictionary objectForKey:@"steps"];

                    for(int i=0;i<[stepsArray count];i++)
                    {
                        NSDictionary *forthTempDictionary = [stepsArray objectAtIndex:i];

                        if([forthTempDictionary objectForKey:@"html_instructions"]!=nil)
                        {
                            NSString * directionStr =[forthTempDictionary objectForKey:@"html_instructions"];

                            NSRange range;
                            while ((range = [directionStr rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound){
                                directionStr=[directionStr stringByReplacingCharactersInRange:range withString:@""];
                            }
                            [directionStrAry addObject:directionStr];
                        }

                        NSDictionary *fifthTempDictionary = [forthTempDictionary objectForKey:@"polyline"];
                        if([fifthTempDictionary objectForKey:@"points"]!=nil)
                        {
                            NSString * routePoint =[fifthTempDictionary objectForKey:@"points"];

                            [polylineSetAry addObject:routePoint];

                           // encodedPoints = [fifthTempDictionary objectForKey:@"points"];
                        }
                        NSDictionary *sixthTempDictionary =[forthTempDictionary objectForKey:@"distance"];
                        if([sixthTempDictionary objectForKey:@"text"]!=nil)
                        {
                            NSString * distanceStr =[sixthTempDictionary objectForKey:@"text"];

                            [distanceStrAry addObject:distanceStr];

                            // encodedPoints = [fifthTempDictionary objectForKey:@"points"];
                        } 
                    }
                }
            }
        }  
    }

    NSLog(@"routeSetAry is :%@",routeSetAry);
 NSLog(@"polylineSetAry is : %i",polylineSetAry.count);

this will gives you array of direction string and you can use this string to diplay anywhere in your project.

In my project i have created a small view that display on map top and in that view i am displaying the direction one by one.

Upvotes: 1

Related Questions