1. 程式人生 > >h5移動端獲取當前位置資訊

h5移動端獲取當前位置資訊

 function getPosition(){

        //判斷瀏覽器是否支援HTML5 定位

      if(window.navigator.geolocation){

        navigator.geolocation.getCurrentPosition(successfulCallback,failCallback)

      }else{

           alert("你的瀏覽器不能使用HTML5定位")

         }

     }

    function successfulCallback(position){

      var latitude = position.coords.latitude;

      var longitude = position.coords.longitude;
alert("成功獲取位置資訊");

    }

    function failCallback(error){

      var text ;

      switch(error.code){

        case error.PERMISSION_DENIED:

              text ="使用者拒絕對獲取地理位置的請求。";

              break;
            case error.POSITION_UNAVAILABLE:

              text ="位置資訊是不可用的。";

              break;

           case error.TIMEOUT:

             text ="請求使用者地理位置超時。";

              break;

           case error.UNKNOWN_ERROR:

              text ="未知錯誤。";

              break;
          }

      }
getPosition();