วันนี้เราจะมาแนะนำ ในส่วนของ Geo-location
Geo-location นั้นเป็นการระบุที่อยู่ของผู้ใช้งาน
โดยถ้า Device ที่เป็น Mobile ก็จะใช้ GPS ในการระบุ
แต่ถ้าเป็น Wifi ก็จะใช้ตำแหน่งที่ใกล้เคียงจากผู้ให้บริการ
มาเริ่มกันเลย !
( จากที่ศึกษามานั้น Geo-location ใช้เฉพาะ HTML5 นะครับ )
โดยเราใช้ Code ของ Javascript
<script>
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition( function(position) {
pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
}, function(errorCode) { // ส่วนของ Error handle
// handleLocationError(true, infoWindow, map.getCenter());
},{
// เพิ่ม Option
maximumAge: Infinity, timeout: 6000, enableHighAccuracy: true
});
}
</script>
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition( function(position) {
pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
}, function(errorCode) { // ส่วนของ Error handle
// handleLocationError(true, infoWindow, map.getCenter());
},{
// เพิ่ม Option
maximumAge: Infinity, timeout: 6000, enableHighAccuracy: true
});
}
</script>
เมื่อมีการเรียกใช้งาน จะปรากฎ Popup ดังรูป
เพียงเท่านี้ คุณก็สามารถนำค่า Lat , Lng ไปใช้งานได้แล้ว
position.coords.latitude , position.coords.longitude
คุณสามารถศึกษา Google Map Web API เพิ่มเติมได้ที่
https://developers.google.com/maps/documentation/javascript/
และขอ Google Map API Key ได้ที่ https://console.developers.google.com/

