简体   繁体   中英

Google maps in UIWebview shows driving directions not the map directly

I am loading google maps in UIWebview by providing the lat long co-ordinates for the Source and destination.But the problem is it shows the driving directions when it gets loaded.If we want to see the map then we have to click on the map button provided alongside the address.I need to directly show the map instead of driving directions when the web view gets loaded. Can any one tell me how do I achieve this.

 UIWebView *webView=[[UIWebView alloc]initWithFrame:webViewRect];
                webView.delegate=self;
                webView.scalesPageToFit=YES;

                CLLocationCoordinate2D start = { 34.052222, -118.243611 };
                CLLocationCoordinate2D destination = { 37.322778, -122.031944 };        
                //NSString *urlString=@"http://maps.google.co.in/";

                NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f",
                                                 start.latitude, start.longitude, destination.latitude, destination.longitude];
                NSLog(@"URL string-----> %@",googleMapsURLString);



                NSURL *url=[NSURL URLWithString:googleMapsURLString];
                NSURLRequest *requestObj=[NSURLRequest requestWithURL:url];
                [webView loadRequest:requestObj];
                [self.view addSubview:webView];

Also I would like to know how to I pass the url if i need to go from say place A to B and from B to C.

Just change your URL line

NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f", start.latitude, start.longitude, destination.latitude, destination.longitude];

with the below code

NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f&output=embed",start.latitude, start.longitude, destination.latitude, destination.longitude];

The only thing added is output=embed which forces the web to open the map directly without opening the screen which asks for filled input box for source and destination.

For more clarifications you can also check out all the parameters that are used in google map from Google Map Parameters

Above will solve your first query.

And regarding second query ie Also I would like to know how to I pass the url if i need to go from say place A to B and from B to C. Sorry no idea

    NSString *ll = [NSString stringWithFormat:@"%f,%f",
                        self.placemark.location.coordinate.latitude,
                        self.placemark.location.coordinate.longitude];
    ll = [ll stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *url = [NSString stringWithFormat:@"http://maps.google.com/?q=%@", ll];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];

In perusing the Google Map Parameters documentation, linked above, I came across the answer to the second part of this question.

Also I would like to know how to I pass the url if i need to go from say place A to B and from B to C.

The Directions portion of the parameter space appears to allow multiple destinations to be routed in sequence by appending "+to:" clauses to the daddr component of the query arguments.

For instance:

https://maps.google.com/maps?saddr=Ferry+Plaza+SF&daddr=Coit+Tower+to:Union+Square

shows a route beginning at the Ferry Plaza in San Francisco which then goes to Coit Tower, then Union Square, in that order.

Read about the 'daddr=' parameter in the above-linked Directions section of that wiki for further info.

In general, you can figure out what Google Maps URLs should look like through parameter inspection. Eg:

  • Go to maps.google.com and query for a map with the routes, destinations, etc. that you are interested in.
  • When you have a map that looks like what you want, click the link icon (looks like a chain) to get a copy of the URL that corresponds to the parameters you've specified.
  • Paste this URL into the browser address bar.
  • Proceed to parse out parameters from the query string to determine which parts correspond to what on the map.
  • Cross-reference with pages like the mapki.com link above if you aren't sure what a parameter is doing.

You should be able to deduce formulation of new kinds of queries using this process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM