HTML5 Geolocation API ช่วยให้คุณแบ่งปันตำแหน่งของคุณกับเว็บไซต์โปรดของคุณ JavaScript สามารถจับละติจูดและลองจิจูดของคุณ และสามารถส่งไปยังเว็บเซิร์ฟเวอร์แบ็กเอนด์ และทำสิ่งต่าง ๆ ที่เกี่ยวข้องกับการรู้ตำแหน่ง เช่น ค้นหาธุรกิจในท้องถิ่นหรือแสดงตำแหน่งของคุณบนแผนที่
Geolocation APIs ทำงานร่วมกับคุณสมบัติใหม่ของวัตถุเนวิเกเตอร์ส่วนกลางเช่น วัตถุตำแหน่งทางภูมิศาสตร์
เมธอด getCurrentPosition จะดึงข้อมูลตำแหน่งทางภูมิศาสตร์ปัจจุบันของอุปกรณ์ ตำแหน่งจะแสดงเป็นชุดพิกัดทางภูมิศาสตร์พร้อมกับข้อมูลเกี่ยวกับหัวเรื่องและความเร็ว ข้อมูลตำแหน่งจะถูกส่งกลับไปยังออบเจกต์ Position

ตัวอย่าง
คุณสามารถลองเรียกใช้รหัสต่อไปนี้เพื่อค้นหาตำแหน่งปัจจุบัน:
<!DOCTYPE HTML>
<html>
<head>
<script type = "text/javascript">
function showLocation(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
alert("Latitude : " + latitude + " Longitude: " + longitude);
}
function errorHandler(err) {
if(err.code == 1) {
alert("Error: Access is denied!");
}
else if( err.code == 2) {
alert("Error: Position is unavailable!");
}
}
function getLocation(){
if(navigator.geolocation){
// timeout at 60000 milliseconds (60 seconds)
var options = {timeout:60000};
navigator.geolocation.getCurrentPosition(showLocation, errorHandler, options);
} else {
alert("Sorry, browser does not support geolocation!");
}
}
</script>
</head>
<body>
<form>
<input type = "button" onclick = "getLocation();" value = "Get Location"/>
</form>
</body>
</html>