案例:html案例     状态:可编辑再运行    进入竖版
 运行结果 
x
 
1
<!DOCTYPE html>
2
<html>
3
<body>
4
<p id="demo">点击这个按钮,获得您的坐标:</p>
5
<button onclick="getLocation()">试一下</button>
6
<script>
7
var x=document.getElementById("demo");
8
function getLocation()
9
  {
10
  if (navigator.geolocation)
11
    {
12
    navigator.geolocation.getCurrentPosition(showPosition,showError);
13
    }
14
  else{x.innerHTML="Geolocation is not supported by this browser.";}
15
  }
16
function showPosition(position)
17
  {
18
  x.innerHTML="Latitude: " + position.coords.latitude + 
19
  "<br />Longitude: " + position.coords.longitude;  
20
  }
21
function showError(error)
22
  {
23
  switch(error.code) 
24
    {
25
    case error.PERMISSION_DENIED:
26
      x.innerHTML="User denied the request for Geolocation."
27
      break;
28
    case error.POSITION_UNAVAILABLE:
29
      x.innerHTML="Location information is unavailable."
30
      break;
31
    case error.TIMEOUT:
32
      x.innerHTML="The request to get user location timed out."
33
      break;
34
    case error.UNKNOWN_ERROR:
35
      x.innerHTML="An unknown error occurred."
36
      break;
37
    }
38
  }
39
</script>
40
</body>
41
</html>
42