Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google maps in UIWebview shows driving directions not the map directly

Tags:

iphone

ipad

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.

like image 837
Kamlesh Raghani Avatar asked Jan 20 '23 06:01

Kamlesh Raghani


2 Answers

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 i.e. 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

like image 150
Suresh Varma Avatar answered Mar 03 '23 04:03

Suresh Varma


    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];
like image 43
Akshay Avatar answered Mar 03 '23 02:03

Akshay