编程笔记 html5&css&js 035 HTML 地理定位

编程笔记 html5&css&js 035 HTML 地理定位

  • [一、HTML5 地理定位](#一、HTML5 地理定位)
  • 二、处理错误和拒绝
  • [三、getCurrentPosition() 方法 - 返回数据](#三、getCurrentPosition() 方法 - 返回数据)
  • [四、Geolocation 对象 - 其他有趣的方法](#四、Geolocation 对象 - 其他有趣的方法)
  • 五、练习
  • 小结

地理定位有很多事情有关,购物时要知道你在哪吧,驾车时就更是了,所有浏览器还提供了定位的功能,可以在网页中使用。

一、HTML5 地理定位

HTML5 Geolocation(地理定位)用于定位用户的位置。

HTML5 Geolocation API 用于获得用户的地理位置。

鉴于该特性可能侵犯用户的隐私,除非用户同意,否则用户位置信息是不可用的。

注释:对于拥有 GPS 的设备,比如 iPhone,地理定位更加精确。

请使用 getCurrentPosition() 方法来获得用户的位置。

下例是一个简单的地理定位实例,可返回用户位置的经度和纬度。

html 复制代码
<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 +
  "<br />Longitude: " + position.coords.longitude;
  }
</script>

检测是否支持地理定位

如果支持,则运行 getCurrentPosition() 方法。如果不支持,则向用户显示一段消息。

如果getCurrentPosition()运行成功,则向参数showPosition中规定的函数返回一个coordinates对象

showPosition() 函数获得并显示经度和纬度

上面的例子是一个非常基础的地理定位脚本,不含错误处理。

二、处理错误和拒绝

getCurrentPosition() 方法的第二个参数用于处理错误。它规定当获取用户位置失败时运行的函数:

实例

function showError(error)

{

switch(error.code)

{

case error.PERMISSION_DENIED:

x.innerHTML="User denied the request for Geolocation."

break;

case error.POSITION_UNAVAILABLE:

x.innerHTML="Location information is unavailable."

break;

case error.TIMEOUT:

x.innerHTML="The request to get user location timed out."

break;

case error.UNKNOWN_ERROR:

x.innerHTML="An unknown error occurred."

break;

}

}

Permission denied - 用户不允许地理定位

Position unavailable - 无法获取当前位置

Timeout - 操作超时

三、getCurrentPosition() 方法 - 返回数据

若成功,则 getCurrentPosition() 方法返回对象。始终会返回 latitude、longitude 以及 accuracy 属性。如果可用,则会返回其他下面的属性。

html 复制代码
属性	描述
coords.latitude	十进制数的纬度
coords.longitude	十进制数的经度
coords.accuracy	位置精度
coords.altitude	海拔,海平面以上以米计
coords.altitudeAccuracy	位置的海拔精度
coords.heading	方向,从正北开始以度计
coords.speed	速度,以米/每秒计
timestamp	响应的日期/时间

四、Geolocation 对象 - 其他有趣的方法

watchPosition() - 返回用户的当前位置,并继续返回用户移动时的更新位置(就像汽车上的 GPS)。

clearWatch() - 停止 watchPosition() 方法

下面的例子展示 watchPosition() 方法。您需要一台精确的 GPS 设备来测试该例(比如 iPhone):

html 复制代码
<script>
var x=document.getElementById("demo");
function getLocation()
  {
  if (navigator.geolocation)
    {
    navigator.geolocation.watchPosition(showPosition);
    }
  else{x.innerHTML="Geolocation is not supported by this browser.";}
  }
function showPosition(position)
  {
  x.innerHTML="Latitude: " + position.coords.latitude +
  "<br />Longitude: " + position.coords.longitude;
  }
</script>

五、练习

html 复制代码
<!DOCTYPE html>
<html lang="zh-cn">
    <title>编程笔记 html5&css&js HTML MathML</title>
    <meta charset="utf-8" />
    <style>
        body {
            color: cyan;
            background-color: teal;
        }
        .container {
            width: 500px; /* 设置容器的宽度 */
            margin: 0 auto; /* 将左右边距设置为自动 */
            line-height: 2;
        }
    </style>
    <body>
        <div class="container">
            <p id="demo">点击这个按钮,获得您的地理定位:</p>
            <button onclick="getLocation()">试一下</button>
            <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.";
                    }
                }
                //coords.latitude	十进制数的纬度
                // coords.longitude	十进制数的经度
                // coords.accuracy	位置精度
                // coords.altitude	海拔,海平面以上以米计
                // coords.altitudeAccuracy	位置的海拔精度
                // coords.heading	方向,从正北开始以度计
                // coords.speed	速度,以米/每秒计
                // timestamp	响应的日期/时间
                function showPosition(position) {
                    x.innerHTML =
                        "纬度: " +
                        position.coords.latitude +
                        "<br />经度: " +
                        position.coords.longitude +
                        "<br />位置精度: " +
                        position.coords.accuracy +
                        "<br />海拔: " +
                        position.coords.altitude +
                        "<br />海拔精度: " +
                        position.coords.altitudeAccuracy +
                        "<br />方向: " +
                        position.coords.heading +
                        "<br />速度(米/秒): " +
                        position.speed +
                        "<br />定位时间: " +
                        position.timestamp;
                }
            </script>
        </div>
    </body>
</html>

小结

实际应用中还要结全地理应用,才能产生更为直观的感受。

相关推荐
柏箱1 分钟前
使用JavaScript写一个网页端的四则运算器
前端·javascript·css
一颗花生米。3 小时前
深入理解JavaScript 的原型继承
java·开发语言·javascript·原型模式
学习使我快乐013 小时前
JS进阶 3——深入面向对象、原型
开发语言·前端·javascript
bobostudio19953 小时前
TypeScript 设计模式之【策略模式】
前端·javascript·设计模式·typescript·策略模式
勿语&4 小时前
Element-UI Plus 暗黑主题切换及自定义主题色
开发语言·javascript·ui
一路向前的月光9 小时前
Vue2中的监听和计算属性的区别
前端·javascript·vue.js
长路 ㅤ   9 小时前
vue-live2d看板娘集成方案设计使用教程
前端·javascript·vue.js·live2d
Fan_web9 小时前
jQuery——事件委托
开发语言·前端·javascript·css·jquery
安冬的码畜日常9 小时前
【CSS in Depth 2 精译_044】第七章 响应式设计概述
前端·css·css3·html5·响应式设计·响应式
Jiaberrr10 小时前
Element UI教程:如何将Radio单选框的圆框改为方框
前端·javascript·vue.js·ui·elementui