Skip to content

Instantly share code, notes, and snippets.

@pjaudiomv
Last active July 14, 2026 15:14
Show Gist options
  • Select an option

  • Save pjaudiomv/c0ce57709f82c4e980aeb8f35aead920 to your computer and use it in GitHub Desktop.

Select an option

Save pjaudiomv/c0ce57709f82c4e980aeb8f35aead920 to your computer and use it in GitHub Desktop.
Platform-aware directions URL helper JS
/**
* @typedef {'web' | 'ios' | 'android'} Platform
*/
/**
* @returns {Platform}
*/
export function getPlatform() {
if (typeof window === 'undefined') return 'web';
const ua = window.navigator.userAgent.toLowerCase();
if (/android/.test(ua)) return 'android';
if (/iphone|ipad|ipod/.test(ua) || (window.navigator.platform === 'MacIntel' && window.navigator.maxTouchPoints > 1)) return 'ios';
return 'web';
}
/**
* @param {{ latitude: number, longitude: number }} location
* @returns {string}
*/
export function getDirectionsUrl(location) {
const lat = location.latitude;
const lng = location.longitude;
const platform = getPlatform();
if (platform === 'ios') {
return `maps://?daddr=${lat},${lng}`;
} else if (platform === 'android') {
return `geo:${lat},${lng}?q=${lat},${lng}`;
}
return `https://www.google.com/maps/dir/?api=1&destination=${lat},${lng}`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment