Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> HTML

HTML5 Geolocation โดยไม่มีการเชื่อมต่อ SSL


HTML5 Geolocation ใช้หาตำแหน่งผู้ใช้ สามารถทำงานได้โดยไม่ต้องมีการเชื่อมต่อ SSL สร้างได้ดังนี้:

// Variable apigeo shows successful location:
var apigeo = function(position) {
  alert("API geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
};
var tryapigeo = function() {
  jQuery.post(
    "check the location on google map",
    function(success) {
      apiGeolocationSuccess({coords: {latitude: success.location.lat, longitude: success.location.lng}});
    }) // Gives success on given geolocation
    .fail(function(err) {
      // On failure, alert with failure is shown
      alert("error while showing geolocation! \n\n" + err);
    });
};

จากนั้นสร้าง switch cases ต่างๆ สำหรับรหัสข้อผิดพลาด รหัสข้อผิดพลาดต่างกันสำหรับข้อผิดพลาดต่างกัน

var tryloc = function() {
// tryloc variable show current position
if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(
    browserGeolocationSuccess, browserGeolocationFail,{
      maximumAge: 70000, timeout: 40000, enableHighAccuracy: true});
  // this will show result with high accuracy
}};