Reputation: 2430
Have a look at the below code it's working properly
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=%f,%f&saddr=%f,%f", 23.0300, 72.5800, 22.3000, 70.7833];
NSString *escapedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:escapedString];
[[UIApplication sharedApplication]openURL:url];
but it opens map in safari browser.
after that i've tried below code
NSString *urlString = [NSString stringWithFormat:@"http://maps.apple.com/maps?daddr=%f,%f&saddr=%f,%f", 23.0300, 72.5800, 22.3000, 70.7833];
NSString *escapedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:escapedString];
[[UIApplication sharedApplication]openURL:url];
it open map app as i desired but not giving me the directional result with route or the same result that above google map gives how can i achieve the result as first code in map app?
please help!!
Upvotes: 0
Views: 312
Reputation: 69499
Apple's map does not have the same API as google maps, thus you URL will not work. With iOS 6 apple introduced the MKMapItem
which allows developers to interact with the maps.app.
If you want to keep using the the maps via http then you should change you url:
NSString *urlString = [NSString stringWithFormat:@"http://maps.apple.com/?daddr=%f,%f&saddr=%f,%f", 23.0300, 72.5800, 22.3000, 70.7833];
NSString *escapedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:escapedString];
[[UIApplication sharedApplication]openURL:url];
As stated in the Apple URL Scheme Reference you should not add the /maps/
path.
URLs that contain no path parameters or that contain specific map paths are opened in Safari and displayed there. For example, URLs based on the paths http://maps.apple.com/, http://maps.apple.com/maps, http://maps.apple.com/local, and http://maps.apple.com/m are all opened in Safari. To open a URL in the Maps app, the path must be of the form http://maps.apple.com/?q.
The rules for creating a valid map link are as follows:
- The domain must be maps.apple.com.
- The path cannot be /maps/*.
- A parameter cannot be q=* if the value is a URL (so KML is not picked up).
- The parameters cannot include view=text or dirflg=r
Upvotes: 2