zilongxuan001 / LearnFreecode

0 stars 0 forks source link

Get Geolocation Data #311

Open zilongxuan001 opened 6 years ago

zilongxuan001 commented 6 years ago

介绍

另外一个很酷的事情就是定位。每个浏览器都内置了导航,可以给我这个信息。

当好会给我们目前的经度(longitude)和纬度(latitude)。

你会看到试图允许或阻止这个网站知道你目前的位置。只要程序正确,挑战就能完成。

如果你选择允许,你将会看到右侧手机输出的文字为你当前所在位置的经纬度

方法

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(function(position) {
    $("#data").html("latitude: " + position.coords.latitude + "<br>longitude: " + position.coords.longitude);
  });
}

代码


<script>
  // Only change code below this line.
  if(navigator.geolocation){
    navigator.geolocation.getCurrentPosition(function(position){
      $("#data").html("latitude:" + position.coords.latitude + "<br>longitude:"+position.coords.longitude);
    })
  }

  // Only change code above this line.
</script>
<div id = "data">
  <h4>You are here:</h4>

</div>

结果显示

image

来源

https://www.freecodecamp.org/challenges/get-geolocation-data