Last active
July 14, 2026 15:14
-
-
Save pjaudiomv/c0ce57709f82c4e980aeb8f35aead920 to your computer and use it in GitHub Desktop.
Platform-aware directions URL helper JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @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