Last active
June 21, 2017 20:31
-
-
Save IronGhost63/96b1cf93ce46b7edad9e764570d85d88 to your computer and use it in GitHub Desktop.
Generate map link according to accessing OS
This file contains 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
/** | |
* Determine the mobile operating system. | |
* This function returns one of 'iOS', 'Android', 'Windows Phone', or 'unknown'. | |
* | |
* @returns {String} | |
* | |
* Source: http://stackoverflow.com/questions/21741841/detecting-ios-android-operating-system | |
*/ | |
function getMobileOperatingSystem() { | |
var userAgent = navigator.userAgent || navigator.vendor || window.opera; | |
// Windows Phone must come first because its UA also contains "Android" | |
if (/windows phone/i.test(userAgent)) { | |
return "Windows Phone"; | |
} | |
if (/android/i.test(userAgent)) { | |
return "Android"; | |
} | |
// iOS detection from: http://stackoverflow.com/a/9039885/177710 | |
if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) { | |
return "iOS"; | |
} | |
return "unknown"; | |
} | |
/** | |
* Generate map uri according to accessing OS | |
* | |
* @param string|int lat given latitude | |
* @param string|int long given longitude | |
* | |
* @return string generated uri scheme | |
*/ | |
function generate_map_link(lat, long){ | |
let device = getMobileOperatingSystem(); | |
let protocol = ""; | |
if(device == 'Android'){ | |
protocol = "geo:"; | |
}else if(device == 'iOS'){ | |
protocol = "http://maps.apple.com/?ll="; | |
}else if(device == 'Windows Phone'){ | |
protocol = "bingmaps:?cp="; | |
}else{ | |
protocol = "http://maps.google.com/maps?q="; | |
} | |
return protocol+lat+","+long; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment