html JS.Geolocate.Longitude.Latitude

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html JS.Geolocate.Longitude.Latitude相关的知识,希望对你有一定的参考价值。

JS.Geolocate.Longitude.Latitude
-------------------------------


A [Pen](https://codepen.io/Onlyforbopi/pen/RYzarJ) by [Pan Doul](https://codepen.io/Onlyforbopi) on [CodePen](https://codepen.io).

[License](https://codepen.io/Onlyforbopi/pen/RYzarJ/license).
<!DOCTYPE html>
<html lang="en">
  <head>
    
<!---
Line 15 checks if the Web browser supports the geolocation API by testing the variable navigator.geolocation. If not null, then the geolocation API is supported.
Line 16 calls navigator.geolocation.getCurrentPosition(showPosition) passing a callback function as a parameter (in this example we did not specify a callback in case of error). When a current position is available, the callback function will be called asynchronously, and the input parameter of this callback function will be the current position, like in the function showPosition(position) of the example.
Line 22: the position objects has a coords property that is the object that holds the longitude and the latitude.

-->
    
    <title>Basic example of use of the geolocation API</title>
    <meta charset="utf-8">
  </head>
<body>
<p id="msg">Click the button to get your coordinates:</p>
<button onclick="getLocation()">Where am I ?</button>
  
<script>
var displayCoords=document.getElementById("msg");
  
function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
  } else {
    displayCoords.innerHTML="Geolocation API not supported by your browser.";
  }
}
  
  
function showPosition(position) {
 displayCoords.innerHTML="Latitude: " + position.coords.latitude + 
  "<br />Longitude: " + position.coords.longitude;	
}
</script>
</body>
</html>

以上是关于html JS.Geolocate.Longitude.Latitude的主要内容,如果未能解决你的问题,请参考以下文章

html Html模板/ Html Boilerplate |标签HTML

html里怎么引用一个html的头部

html5与传统html区别

html4和html5的区别

HTML元素

head标签怎么给多个html引用