-1

I am using google map sdk and now i want to open get direction in google map where there are multiple stops like i am at Location A and from that i want to go to locations B,C,D i am able to open google map with location A to B but not able to open it for A to B,C,D. How can i do this i tried this

NSString *str1 =[NSString stringWithFormat:@"http://maps.google.com/?saddr=%@&daddr=%@&waypoints=%@&key=%@",originString,destinationString,strWayPoints,GOOGLE_API_KEY];
if([[UIApplication sharedApplication] canOpenURL:
    [NSURL URLWithString:@"comgooglemaps://"]])
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str1]];
}
//str1 =
http://maps.google.com/?saddr=18.518205,73.857431&daddr=18.518205,73.857431&waypoints=via:18.518205,73.857431|via:18.552248,73.901596|via:18.629764,73.934685&key=MYKEY
xomena
  • 31,125
  • 6
  • 88
  • 117
Yuvraj Kale
  • 49
  • 1
  • 9

2 Answers2

3

You should use Google Maps URLs that provides universal cross platform syntax to open maps in mobile apps or Google Maps website.

In directions mode, you can specify the origin, destination and multiple waypoints for your route. For more details have a look at the following page:

https://developers.google.com/maps/documentation/urls/guide#directions-action

Example of URL:

https://www.google.com/maps/dir/?api=1&origin=18.518205,73.857431&destination=18.518205,73.857431&waypoints=18.518205,73.857431%7C18.552248,73.901596%7C18.629764,73.934685

I hope this helps!

xomena
  • 31,125
  • 6
  • 88
  • 117
  • Hello @xomena , Solution you given to me is not opening the google map on actual device when i do this `[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:nil completionHandler:^(BOOL success) { if(success) { NSLog(@"Google Map open"); } }];` It always return **NO** . Any solution? – Yuvraj Kale Aug 30 '17 at 04:58
  • URL You provided is opening google map on browser but when i am trying to open it from iphone my app to google map its not working.Why? – Yuvraj Kale Sep 27 '17 at 07:45
1

This is code which is now working fine for get direction. I am answering here so that other will get how to use it

- (IBAction)onClickNavigate:(id)sender {

NSString *strWayPoints = [NSString stringWithFormat:@"%f,%f", [[destLatArray objectAtIndex:0] doubleValue], [[destLongArray objectAtIndex:0] doubleValue]];
for(int j=0;j<destLatArray.count;j++){
    if(j > 0)
        strWayPoints = [NSString stringWithFormat:@"%@|%f,%f", strWayPoints, [[destLatArray objectAtIndex:j] doubleValue], [[destLongArray objectAtIndex:j] doubleValue]];

}
NSString *originString = [NSString stringWithFormat:@"%f,%f",[sourceLat doubleValue], [sourceLong doubleValue]];
NSString *destinationString = [NSString stringWithFormat:@"%f,%f", [[destLatArray objectAtIndex:0] doubleValue], [[destLongArray objectAtIndex:0] doubleValue]];

NSString *str = [NSString stringWithFormat:@"https://www.google.com/maps/dir/?api=1&origin=%@&destination=%@&waypoints=%@",originString,destinationString,strWayPoints];

if([[UIApplication sharedApplication] canOpenURL:
    [NSURL URLWithString:@"comgooglemaps://"]])
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}
else
{
    NSLog(@"You haven't installed the google map");
}

}

Yuvraj Kale
  • 49
  • 1
  • 9