Skip to content

Instantly share code, notes, and snippets.

@joshualambert
Created October 20, 2017 13:33
Show Gist options
  • Save joshualambert/ba5d6bbe98bc9b8124e8a04648df9d20 to your computer and use it in GitHub Desktop.
Save joshualambert/ba5d6bbe98bc9b8124e8a04648df9d20 to your computer and use it in GitHub Desktop.
Titanium Navigation Function
// Used to prompt a user for how to open an address, or open automatically, based on the platform.
function openAddress(destAddr, startAddr) {
var destParam = '';
var startParam = '';
if (typeof destAddr !== 'undefined') {
destParam = '&daddr=' + encodeURIComponent(destAddr);
}
if (typeof startAddr !== 'undefined') {
startParam = '&saddr=' + encodeURIComponent(startAddr);
}
if (OS_IOS) { // iOS. Prompt user if they want Apple Maps or Google Maps.
var infoDialog = Titanium.UI.createAlertDialog({
title: 'Info',
message: 'Please select which map you want to use: ',
buttonNames: ['Google Maps', 'Apple Maps', 'Cancel'],
cancel:2
});
infoDialog.addEventListener('click', function(e) {
if (e.index === 1) { // Apple Maps was chosen.
Ti.Platform.openURL("http://maps.apple.com/?linkSrc=mobileApp" + startParam + destParam);
} else if (e.index === 0) { // Google Maps was chosen.
Ti.Platform.openURL("http://maps.google.com/maps?linkSrc=mobileApp" + startParam + destParam + '&dirflg=r&mra=ltm&t=m'); // Google Maps.
}
});
infoDialog.show();
} else if (OS_ANDROID) { // Android. Open Google Maps.
var mapIntent = Ti.Android.createIntent({
action : Ti.Android.ACTION_VIEW,
data : "http://maps.google.com/maps?linkSrc=mobileApp" + startParam + destParam + '&dirflg=r&mra=ltm&t=m'
});
Ti.Android.currentActivity.startActivity(mapIntent);
} else { // Unknown platform. Just open the browser to Google Maps.
Ti.Platform.openURL("http://maps.google.com/?linkSrc=mobileApp" + startParam + destParam + '&dirflg=r&mra=ltm&t=m');
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment