<div class="container">
    <div id="alertPlaceholder"></div>
    <h4>Geolocation:</h4>
    <button id="request-location">Request location. ร้องขอตำแหน่งอีกครั้ง.</button>
    <div id="geolocationResult"></div>
</div>
/* Format page styles */
body {
    font-family: sans-serif;
    font-size: 16px;
    line-height: 1;
}
button {
    background-color: #ddd;
    border: 1px solid #bbb;
    border-radius: 5px;
    cursor: pointer;
    font-size: 1rem;
    padding: 3px 5px;
}
button:hover {
    background-color: #eee;
}
button:focus,
button:active {
    outline: 2px solid #1E88E5;
}
code {
    background-color: #eee;
    padding: 2px 3px;
}
p {
    line-height: 1.5rem;
}
.container {
    margin: 10px auto;
    width: 80vw;
}
@media (min-width: 1200px) {
    .container {
        width: 1000px;
    }
}
.alert {
    margin-bottom: 20px;
    padding: 10px;
}
.alert.info {
    background-color: #80DEEA;
}
.alert.error {
    background-color: #f00;
    color: #fff;
}
/* End format page styles */

let alertPlaceholder = document.getElementById('alertPlaceholder');
let geolocationResult = document.getElementById('geolocationResult');
if('geolocation' in navigator) {
    // if geolocation is supported.
    let alertGeolocation = '<div class="alert info">Geolocation is available. เบราว์เซอร์นี้รองรับ Geolocation.</div>';
    alertPlaceholder.insertAdjacentHTML('beforeend', alertGeolocation);

    // request for location.
    requestLocation();
} else {
    // if geolocation is NOT supported.
    let alertGeolocation = '<div class="alert error">Geolocation is NOT available. เบราว์เซอร์นี้ไม่รองรับ Geolocation.</div>';
    alertPlaceholder.insertAdjacentHTML('beforeend', alertGeolocation);
}

// listen on click request location button.
document.getElementById('request-location').addEventListener('click', (e) => {
    e.preventDefault();
    requestLocation();
});


/**
 * Request location
 *
 * @link https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition `getCurrentPosition()` parameters.
 */
function requestLocation() {
    let options = {
        enableHighAccuracy: true
    };
    navigator.geolocation.getCurrentPosition(showPosition, showError, options);
}


/**
 * Show position
 */
function showPosition(position) {
    let coords = position.coords;

    // clear previous result.
    alertPlaceholder.innerHTML = '';
    geolocationResult.innerHTML = '';

    console.log('allowed.', position);
    geolocationResult.insertAdjacentHTML('beforeend', '<p>You have given permission to access the location. คุณได้อนุญาตให้เข้าถึงตำแหน่งแล้ว.</p>');

    let coordsHTML = '<p>';
    for (const property in coords) {
        coordsHTML += '<code>position.coords.' + property + '</code>: ';
        coordsHTML += coords[property];
        coordsHTML += '<br>';
    }
    coordsHTML += '</p>';
    coordsHTML += '<p><a href="http://maps.google.com/maps?z=12&t=m&q=loc:' + coords.latitude + '+' + coords.longitude + '" target="gmaps">Search on Google Maps. ค้นหาบน Google Maps.</a></p>';
    geolocationResult.insertAdjacentHTML('beforeend', coordsHTML);
    
    geolocationResult.insertAdjacentHTML('beforeend', '<p><code>position.timestamp</code>: ' + position.timestamp + '</p>');
}


/**
 * Show errors
 */
function showError(error) {
  switch(error.code) {
    case error.PERMISSION_DENIED:
      alertPlaceholder.insertAdjacentHTML('beforeend', '<div class="alert error">User denied the request for Geolocation. ผู้ใช้ปฏิเสธคำขอ Geolocation.</div>');
      break;
    case error.POSITION_UNAVAILABLE:
      alertPlaceholder.insertAdjacentHTML('beforeend', '<div class="alert error">Location information is unavailable. ข้อมูลตำแหน่งไม่สามารถใช้ได้.</div>');
      break;
    case error.TIMEOUT:
      alertPlaceholder.insertAdjacentHTML('beforeend', '<div class="alert error">The request to get user location timed out. คำขอรับตำแหน่งของผู้ใช้หมดเวลา.</div>');
      break;
    case error.UNKNOWN_ERROR:
      
      alertPlaceholder.insertAdjacentHTML('beforeend', '<div class="alert error">An unknown error occurred. เกิดข้อผิดพลาดที่ไม่รู้จัก.</div>');
      break;
  }
} 

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.