“`” HTML5 地理定位用于定位用户的位置

HTML5 地理定位API用于获取用户的地理位置。

由于这会侵犯用户隐私,除非用户批准,否则该位置不可用。

使用 getCurrentPosition() 方法获取用户的位置。

下面的示例是返回用户位置的纬度和经度的简单地理定位示例:

<pre><code class=""language-javascript"" lang=""javascript""><script> 
var x=document.getElementById(""demo""); 
function getLocation() { 
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else{
x.innerHTML=""Geolocation is not supported by this browser."";


function showPosition(position) {
x.innerHTML=""Latitude: "" + position.coords.latitude +""Longitude: "" + position.coords.longitude;

</script>
</code></pre>

<pre><code> "“`

Was this helpful?

0 / 0

发表回复 0

Your email address will not be published.