如何将地理位置坐标传递给变量?

时间:2020-10-27 13:51:02

标签: javascript function geolocation

我正在尝试将位置线转换为变量,但结果不确定

var a = getLocation();

我尝试过的代码

  <script type = "text/javascript">
          
          var a = getLocation();
          alert(a);
         function showLocation(position) {
            var latitude = position.coords.latitude;
            var longitude = position.coords.longitude;
           return( latitude + "," + longitude);
         }

         function errorHandler(err) {
            if(err.code == 1) {
               alert("Error: Access is denied!");
            } else if( err.code == 2) {
               alert("Error: Position is unavailable!");
            }
         }
            
         function getLocation() {

            if(navigator.geolocation) {
               
               // timeout at 60000 milliseconds (60 seconds)
               var options = {timeout:60000};
               navigator.geolocation.getCurrentPosition(showLocation, errorHandler, options);
            } else {
               alert("Sorry, browser does not support geolocation!");
            }
         }
            
      </script>

0 个答案:

没有答案