ဝဘ် Geolocation API
အသုံးပြုသူ၏ရာထူးကိုရှာပါ။
HTML Geolocation API ကို အသုံးပြုသူတစ်ဦး၏ ပထဝီဝင်အနေအထားကို ရရှိရန် အသုံးပြုသည်။
၎င်းသည် privacy ကိုအလျှော့အတင်းလုပ်နိုင်သောကြောင့်၊ အသုံးပြုသူမှ၎င်းကိုအတည်ပြုမပေးပါကရာထူးမရနိုင်ပါ။
မှတ်ချက်- စမတ်ဖုန်းကဲ့သို့ GPS ပါသည့် စက်များအတွက် Geolocation သည် အတိကျဆုံးဖြစ်သည်။
Geolocation API ကို ဘရောက်ဆာအားလုံးတွင် ပံ့ပိုးထားသည်-
Yes | Yes | Yes | Yes | Yes |
မှတ်ချက်- Chrome 50 တွင် Geolocation API သည် HTTPS ကဲ့သို့သော လုံခြုံသောအကြောင်းအရာများတွင်သာ အလုပ်လုပ်ပါမည်။ အကယ်၍ သင့်ဆိုက်ကို လုံခြုံမှုမရှိသော မူရင်း ( HTTP ကဲ့သို့) တွင် လက်ခံထားပါက သုံးစွဲသူများ၏ တည်နေရာကို ရယူရန် တောင်းဆိုချက်များ လုပ်ဆောင်နိုင်တော့မည် မဟုတ်ပါ။
Geolocation API ကိုအသုံးပြုခြင်း။
အသုံးပြုသူ၏ getCurrentPosition()
အနေအထားကို ပြန်ပေးရန် နည်းလမ်းကို အသုံးပြုသည်။
အောက်ဖော်ပြပါ ဥပမာသည် အသုံးပြုသူ၏ တည်နေရာ၏ လတ္တီတွဒ်နှင့် လောင်ဂျီတွဒ်တို့ကို ပြန်ပေးသည်-
ဥပမာ
<script>
const x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
ဥပမာ ရှင်းပြထားသည်-
- Geolocation ကို ပံ့ပိုးထားခြင်း ရှိမရှိ စစ်ဆေးပါ။
- ထောက်ခံပါက getCurrentPosition() နည်းလမ်းကို လုပ်ဆောင်ပါ။ မဟုတ်ပါက အသုံးပြုသူထံသို့ မက်ဆေ့ချ်တစ်ခု ပြသပါ။
- getCurrentPosition() method အောင်မြင်ပါက၊ ၎င်းသည် parameter (showPosition) တွင်ဖော်ပြထားသော လုပ်ဆောင်ချက်အား သြဒိနိတ်အရာဝတ္တုအား ပြန်ပေးသည်။
- showPosition() လုပ်ဆောင်ချက်သည် Latitude နှင့် Longitude ကိုထုတ်ပေးသည်။
အထက်ဖော်ပြပါ ဥပမာသည် အမှားအယွင်းမရှိသော အလွန်အခြေခံကျသော Geolocation script တစ်ခုဖြစ်သည်။
အမှားများနှင့် ငြင်းပယ်မှုများကို ကိုင်တွယ်ခြင်း။
getCurrentPosition()
အမှားများကို ကိုင်တွယ်ရန် နည်းလမ်း ၏ ဒုတိယ ဘောင်ကို အသုံးပြုသည်။ အသုံးပြုသူ၏တည်နေရာကို ရယူရန် ပျက်ကွက်ပါက ၎င်းသည် လုပ်ဆောင်ရန် လုပ်ဆောင်ချက်ကို သတ်မှတ်ပေးသည်-
ဥပမာ
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred."
break;
}
}
မြေပုံတွင်ရလဒ်ကိုပြသခြင်း။
မြေပုံတစ်ခုတွင် ရလဒ်ကိုပြသရန်၊ သင်သည် Google Maps ကဲ့သို့ မြေပုံဝန်ဆောင်မှုသို့ ဝင်ရောက်ခွင့် လိုအပ်ပါသည်။
အောက်ဖော်ပြပါ ဥပမာတွင်၊ ပြန်ပေးသော လတ္တီတွဒ်နှင့် လောင်ဂျီတွဒ်တို့ကို Google Map တွင် တည်နေရာပြရန် (အငြိမ်ပုံသဏ္ဍာန်ကို အသုံးပြု၍) ကိုအသုံးပြုသည်-
ဥပမာ
function showPosition(position) {
let latlon = position.coords.latitude + "," + position.coords.longitude;
let img_url = "https://maps.googleapis.com/maps/api/staticmap?center=
"+latlon+"&zoom=14&size=400x300&sensor=false&key=YOUR_KEY";
document.getElementById("mapholder").innerHTML = "<img src='"+img_url+"'>";
}
တည်နေရာ-တိကျသောအချက်အလက်
ဤစာမျက်နှာသည် မြေပုံပေါ်တွင် သုံးစွဲသူတစ်ဦး၏ အနေအထားကို မည်သို့ပြသရမည်ကို သရုပ်ပြထားပါသည်။
Geolocation သည် အောက်ပါကဲ့သို့သော တည်နေရာဆိုင်ရာ အချက်အလက်များအတွက် အလွန်အသုံးဝင်ပါသည်။
- နောက်ဆုံးပေါ် ဒေသဆိုင်ရာ အချက်အလက်
- အသုံးပြုသူအနီးတွင် စိတ်ဝင်စားဖွယ်အချက်များ ပြသခြင်း။
- တစ်ကွေ့ပြီးတစ်ကွေ့ လမ်းပြခြင်း (GPS)
getCurrentPosition() နည်းလမ်း - ဒေတာပြန်ပို့ခြင်း။
နည်းလမ်း သည် getCurrentPosition()
အောင်မြင်မှုတွင် အရာဝတ္ထုတစ်ခုကို ပြန်ပေးသည်။ လတ္တီတွဒ်၊ လောင်ဂျီတွဒ်နှင့် တိကျမှု ဂုဏ်သတ္တိများကို အမြဲတမ်း ပြန်ပေးသည်။ ရနိုင်လျှင် အခြားဂုဏ်သတ္တိများကို ပြန်ပေးသည်-
Property | Returns |
---|---|
coords.latitude | The latitude as a decimal number (always returned) |
coords.longitude | The longitude as a decimal number (always returned) |
coords.accuracy | The accuracy of position (always returned) |
coords.altitude | The altitude in meters above the mean sea level (returned if available) |
coords.altitudeAccuracy | The altitude accuracy of position (returned if available) |
coords.heading | The heading as degrees clockwise from North (returned if available) |
coords.speed | The speed in meters per second (returned if available) |
timestamp | The date/time of the response (returned if available) |
Geolocation Object - အခြားစိတ်ဝင်စားဖွယ်နည်းလမ်းများ
Geolocation object တွင် အခြားသော စိတ်ဝင်စားဖွယ် နည်းလမ်းများ လည်း ရှိပါသည်။
watchPosition()
- အသုံးပြုသူ၏ လက်ရှိအနေအထားကို ပြန်ပေးကာ အသုံးပြုသူရွေ့လျားသွားသည့်အတိုင်း (ကားရှိ GPS ကဲ့သို့) မွမ်းမံထားသော အနေအထားသို့ ဆက်လက်ရောက်ရှိနေပါသည်။clearWatch()
-watchPosition()
နည်းလမ်းကိုရပ်ပါ။
အောက်ဖော်ပြပါ ဥပမာသည် watchPosition()
နည်းလမ်းကို ပြသည်။ ဤ (စမတ်ဖုန်းကဲ့သို့) စမ်းသပ်ရန် တိကျသော GPS ကိရိယာတစ်ခု လိုအပ်သည်-
ဥပမာ
<script>
const x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.watchPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>