Open Native Google Maps App from Phonegap / Cordova (iOS)
If you want to show a map in your Phonegap app, you can always use the google maps API to show a map, or even use the in-app browser to link out to a google map page. But nothing quite matches the Google maps native app, with it’s funky satnav-like features, and slick interface
Thing is, the Google Maps App isn’t installed by default, so in this case, I’m failing-over to a in-App Browser version. This code is iOS only, but anyone wishing to provide a port to Android will be rewarded* (Yes, I’d pay for the port)
– (void) openGoogleMaps:(CDVInvokedUrlCommand *)command
{
NSLog(@”openGoogleMaps”);
NSString* callbackId = [command callbackId];
NSArray* arguments = [command arguments];
NSString* map = [arguments objectAtIndex:0];
CDVPluginResult* result;
NSURL *testURL = [NSURL URLWithString:@”comgooglemaps://”];
if ([[UIApplication sharedApplication] canOpenURL:testURL]) {
NSString *directionsRequest = [NSString stringWithFormat:@”%@%@”,
@”comgooglemaps://?” ,
map];
NSURL *directionsURL = [NSURL URLWithString:directionsRequest];
[[UIApplication sharedApplication] openURL:directionsURL];
result = [CDVPluginResult
resultWithStatus:CDVCommandStatus_OK
messageAsBool:true];
} else {
NSLog(@”Can’t use comgooglemaps:// on this device.”);
result = [CDVPluginResult
resultWithStatus:CDVCommandStatus_OK
messageAsBool:false];
}
[self success:result callbackId:callbackId];
}
Then, this is called from Javascript as follows:
function openGoogleMaps(route)
{
console.log(“openGoogleMaps”);
cordova.exec(function(data)
{
console.log(“Returned from google maps:” + data);
if (!data)
{
// Failed to find native app.
var strUrl = “http://maps.google.com/maps?”;
strUrl += route;
var ref = window.open(strUrl, “_blank”, “location=yes”);
}
}, function(data)
{
console.log(“Plugin failure”);
}, ‘StatusBar’, ‘openGoogleMaps’, [route]);
}
An improvement on this is to use the callback feature as follows:
// FIACH: Open Google Maps URL
– (void) openGoogleMaps:(CDVInvokedUrlCommand *)command
{
NSLog(@”openGoogleMaps”);
NSString* callbackId = [command callbackId];
NSArray* arguments = [command arguments];
NSString* map = [arguments objectAtIndex:0];
CDVPluginResult* result;
NSURL *testURL = [NSURL URLWithString:@”comgooglemaps-x-callback://”];
if ([[UIApplication sharedApplication] canOpenURL:testURL]) {
NSString *directionsRequest = [NSString stringWithFormat:@”%@%@%@%@”,
@”comgooglemaps-x-callback://?” ,
map,
@”&x-success=icebreaker://”,
@”&x-source=IceBreaker”];
NSURL *directionsURL = [NSURL URLWithString:directionsRequest];
[[UIApplication sharedApplication] openURL:directionsURL];
result = [CDVPluginResult
resultWithStatus:CDVCommandStatus_OK
messageAsBool:true];
} else {
NSLog(@”Can’t use comgooglemaps:// on this device.”);
result = [CDVPluginResult
resultWithStatus:CDVCommandStatus_OK
messageAsBool:false];
}
[self success:result callbackId:callbackId];
}
where the callback url is defined in your PLIST as follows
CFBundleURLTypes
CFBundleURLSchemes
fb305632926232115
icebreaker
CFBundleURLName
com.letsbreaksomeice.icebreaker
LikeLike