ankyy
ankyy

Reputation: 349

Open google maps through app in safari in iPhone

I want to open the google maps through my app to get the direction with source address equal to my current location and destination address is my string.I have used the below code,i am able to open the app but not able to get the direction.

 NSString *url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=My+Location&daddr=%@", [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];

By using these line of code, i am able to open the app but the map is not taking the current location,destination address is there.

I have used this also by passing current location latitude and longitude but this also showing current location lat and long to 0.0000 both.

  NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%1.6f,%1.6f&daddr=%@",
                         coordinate.latitude, coordinate.longitude,
                         [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];

Upvotes: 0

Views: 2342

Answers (1)

Gad
Gad

Reputation: 2877

This worked for me:

NSString *coordinateString1 = [NSString stringWithFormat:@"%f,%f", coordinate1.latitude, coordinate1.longitude];
NSString *coordinateString2 = [NSString stringWithFormat:@"%f,%f", coordinate2.latitude, coordinate2.longitude];
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%@&daddr=%@",coordinateString1,coordinateString2]];
[[UIApplication sharedApplication] openURL: URL];

Upvotes: 2

Related Questions