I'm trying to launch Google Maps iOS Application in my Application by doing this :
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Google Map";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:
#"comgooglemaps://?center=40.765819,-73.975866&zoom=14&views=traffic"]];
}
However, the app does not launch Google Maps iOS at all. I do have Google Maps iOS installed.
What I am missing over here?
Can somebody help me out?
Thanks.
Try this instead
CLLocationCoordinate2D coordinate = COORDINATE;
// Open Google Maps application
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"comgooglemaps://"]]) {
NSString *daddr = [NSString stringWithFormat:#"%g,%g",coordinate.latitude, coordinate.longitude];
NSString *urlString = nil;
BOOL showQuery = YES;
if (showQuery) {
// Query
urlString = [NSString stringWithFormat:#"comgooglemaps://?q=%#",daddr];
} else {
// Directions
urlString = [NSString stringWithFormat:#"comgooglemaps://?saddr=&daddr=%#", daddr];
}
NSURL *url = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:url];
}
You'll need to include comgooglemaps url scheme to your project Info.plist. I recommend checking out Localide. It helps you open Google Maps, Waze, Apple Maps, Transit App, Citymapper, and Navigon very easily.
Related
I can open Google maps app using [NSURL URLWithString:#"comgooglemaps://...."]
But how to open Navigation mode?
The navigation mode is explained here, "Add navigation to your app" : https://developers.google.com/maps/documentation/ios-sdk/urlscheme
Try this code :
NSURL *testURL = [NSURL URLWithString:#"comgooglemaps-x-callback://"];
if ([[UIApplication sharedApplication] canOpenURL:testURL]) {
NSString *directionsRequest = #"comgooglemaps-x-callback://" + #"?daddr=John+F.+Kennedy+International+Airport,+Van+Wyck+Expressway,+Jamaica,+New+York" + #"&x-success=sourceapp://?resume=true&x-source=AirApp";
NSURL *directionsURL = [NSURL URLWithString:directionsRequest];
[[UIApplication sharedApplication] openURL:directionsURL];
}
I want to open google map naviagation directly through googgle map App.
currenly i am using the code below
NSURL *testURL = [NSURL URLWithString:#"comgooglemaps-x-callback://"];
if ([[UIApplication sharedApplication] canOpenURL:testURL]) {
NSString *direction=[NSString stringWithFormat:#"comgooglemaps-x-callback://?saddr=%#,%#&daddr=%#,%#&x-success=sourceapp://?resume=true&x-source=AirApp", #"18.9750", #"72.8258", #"23.0300",#"72.5800"];
NSURL *directionsURL = [NSURL URLWithString:direction];
[[UIApplication sharedApplication] openURL:directionsURL];
}
else {
showAlert(AlertTitle, #"You don't have GoogleMaps App on this device. Please install it.");
//NSLog(#"Can't use comgooglemaps-x-callback:// on this device.");
}
}
But that is showing only direction and after that i need to click on navigation button in the App , is there is any way so that i can open google map navigation
directly.
I am working on a navigation app which launches GOOGLE MAP from my native app when user click on any of the pins displayed on the apple map. The location data was pulled from GOOGLE. So I used this code for block showing a callout accessory and let user get to the GOOGLE MAP
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
[self.navigationController pushViewController:[[UIViewController alloc] init] animated:YES];
NSString* addr = [NSString stringWithFormat:#"http://maps.google.com/maps?daddr=%1.6f,%1.6f&saddr", view.annotation.coordinate.latitude,view.annotation.coordinate.longitude];
NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
}
Then I found out there is a x-callback url which can put a back button in the google map and let the user go back to my native app. but anyone know how to use these lines of codes? i have registered my app in the properties list but i have no idea what to do the next. the code from google is like this:
comgooglemaps-x-callback://?center=40.765819,-73.975866&zoom=14
&x-success=sourceapp://?resume=true
&x-source=SourceApp
Can anyone please tell me where shall i put these codes? and How to use that in my app?
If you scroll down in the Google documentation they give example code on how to use this in iOS.
Code provided:
NSURL *testURL = [NSURL URLWithString:#"comgooglemaps-x-callback://"];
if ([[UIApplication sharedApplication] canOpenURL:testURL])
{
NSString *directionsRequest = #"comgooglemaps-x-callback://" +
#"?daddr=John+F.+Kennedy+International+Airport,+Van+Wyck+Expressway,+Jamaica,+New+York" +
#"&x-success=sourceapp://?resume=true&x-source=AirApp";
NSURL *directionsURL = [NSURL URLWithString:directionsRequest];
[[UIApplication sharedApplication] openURL:directionsURL];
}
else
{
NSLog(#"Can't use comgooglemaps-x-callback:// on this device.");
}
You would change everthing after daddr= with your address. Also change AirApp to the name of your application.
you should use below code :
NSURL *testURL = [NSURL URLWithString:#"comgooglemaps-x-callback://"];
if ([[UIApplication sharedApplication] canOpenURL:testURL])
{
NSMutableString *directionsRequest = [NSMutableString stringWithString:#"comgooglemaps-x-callback://"];
[directionsRequest appendFormat:#"?daddr=John+F.+Kennedy+International+Airport,+Van+Wyck+Expressway,+Jamaica,+New+York"];
[directionsRequest appendFormat:#"&x-success=CouriorMap://?resume=true&x-source=AirApp"];
NSURL *directionsURL = [NSURL URLWithString:directionsRequest];
[[UIApplication sharedApplication] openURL:directionsURL];
}
else
{
NSLog(#"Can't use comgooglemaps-x-callback:// on this device.");
}
To use the google map call back function:
Add the following content to info.plist
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>com.example.GoogleMapTest</string>
<key>CFBundleURLSchemes</key>
<array>
<string>myapp</string>
</array>
</dict>
</array>
</dict>
</plist>
com.example.GoogleMapTest is you app bundle id.
myapp is the url scheme just like google map's comgooglemaps://.
Call the function anywhere you wish:
- (void)useGoogleMapApp {
if ([[UIApplication sharedApplication] canOpenURL:
[NSURL URLWithString:#"comgooglemaps://"]]) {
NSString *url = #"comgooglemaps-x-callback://?saddr=1.294094,103.853722&daddr=1.244310,103.833386&directionsmode=transit&x-success=myapp://?resume=true&x-source=MyAppName";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
} else {
NSLog(#"Can't use comgooglemaps://");
}
}
I am trying to integrate the most popular navigation apps into my app, and all of those I chose work, except for Sygic.
Following this guide, I wrote the code:
NSString *URL = [NSString stringWithFormat:#"com.sygic.aura://coordinate|%f|%f|drive",
self.coordinate.longitude,
self.coordinate.latitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:URL]];
But when the code is run, Sygic doesn't open, nothing happens.
Checking [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"com.sygic.aura://"]] returns YES when the app is installed and NO when it's not (as it should).
I tested using "Sygic Brasil" and "Sygic (All Regions)", version 13, but neither will open.
I also tried percent-escaping the URL string, and that didn't work either.
you try following code,
NSString *URL = [NSString stringWithFormat:#"com.sygic.aura://coordinate|%f|%f|drive",
self.coordinate.longitude,
self.coordinate.latitude];
NSURL *newURL = [NSURL URLWithString:[URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ];
if(newURL)
{
[[UIApplication sharedApplication] openURL:newURL];
}
else
{
NSLog(#"Something wrong with lat or long or both");
}
For launching video on youtube app, I am using below code.
NSURL *instagramURL = [NSURL URLWithString:#"youtube://foo"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
NSLog(#"opening youtube app...");
NSString *stringURL = #"http://www.youtube.com/watch?v=H9VdapQyWfg";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
} else {
// open in UIWebView in WebViewViewController
WebViewViewController *secondView = [self.storyboard instantiateViewControllerWithIdentifier:#"webinterface"];
secondView.headerLabel = #"YouTube";
secondView.webPath = #"http://www.youtube.com/watch?v=H9VdapQyWfg";
[self.navigationController pushViewController:secondView animated:YES];
}
Now client changed the mind and asking to put channel in iPhone app.
For testing, I used link http://www.youtube.com/user/richarddawkinsdotnet
BUT when I use this link, instead of youtube app, it always opens in SAFARI. :(
Any idea/ suggestion on how can I open channel in YouTube app with link provided?
You're code's going wrong because, although you're checking if you can open the youTube URL more or less correctly, you're then just opening a web address, which will always open in Safari.
This is the code I've just used, which is working for me. You might want to modify the else statement if you want to fallback to using a webviewcontroller as this will open Safari if the youtube app isn't installed.
NSString *channelName = #"TheNameOfTheChannel";
NSURL *linkToAppURL = [NSURL URLWithString:[NSString stringWithFormat:#"youtube://user/%#",channelName]];
NSURL *linkToWebURL = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.youtube.com/user/%#",channelName]];
if ([[UIApplication sharedApplication] canOpenURL:linkToAppURL]) {
// Can open the youtube app URL so launch the youTube app with this URL
[[UIApplication sharedApplication] openURL:linkToAppURL];
}
else{
// Can't open the youtube app URL so launch Safari instead
[[UIApplication sharedApplication] openURL:linkToWebURL];
}
Same answer, but shorter:
if (![[UIApplication sharedApplication] openURL: [NSURL URLWithString: #"youtube://user/%channelName%"]]) {
NSURL *webURL = [NSURL URLWithString:#"http://www.youtube.com/user/%channelName%"];
[[UIApplication sharedApplication] openURL: webURL];
}
and twitter:
if (![[UIApplication sharedApplication] openURL: [NSURL URLWithString: #"twitter://user?screen_name=%channelName%"]]) {
NSURL *webURL = [NSURL URLWithString:#"http://twitter.com/%channelName%"];
[[UIApplication sharedApplication] openURL: webURL];
}
And here is a more exhaustive list of apps:
http://wiki.akosma.com/IPhone_URL_Schemes
youtube://user/<channel-id> stopped working in iOS 9 so i research and found this working well now in iOS 9
youtube://www.youtube.com/channel/<channel-id>