HTML5中怎么使用地理位置实现定位功能

发布时间:2022-03-09 10:54:57 作者:iii
来源:亿速云 阅读:204

本篇内容介绍了“HTML5中怎么使用地理位置实现定位功能”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

我们可以通过navigator.geolocation获取地理位置对象,他提供了下列方法:

getCurrentPosition(回调,errorCallback,选项):获取当前位置;

watchPosition(回调,错误的选项):开始监控当前位置;

clearWatch(ID):停止监控当前位置

注:下面例子使用的浏览器是铬,使用其他浏览器我不能保证运行结果和例子显示的结果一致

1.当前电子杂志位置

我们将使用getCurrentPosition方法获取当前位置,位置信息不会以结果的形式直接返回,我们需要使用回调函数进行处理。

复制代码代码如下: 

<!DOCTYPE HTML>

<html>

<head>

<title>示例</ title>

<style>

表{border-colla ps e:塌陷;}

th,td {padding:4px;}

th {text-align:right; }

</ style>

</ head>

<body>

<table border =“ 1”>

<tr>

<th>经度:</ th>

<td id =“ longitude”>-</ td>

<th>纬度: </ th>

<td id =“ latitude”>-</ td>

</ tr>

<tr>

<th>海拔高度:</ th>

<td id =“ altitude”>-</ td>

<th>准确性:</ th>

<td id =“精度“>-</ td>

</ tr>

<tr>

<th>海拔精度:</ th>

<td id =” altitudeAccuracy“>-</ td>

<th>标题:</ th>

<td id =“ heading”>-</ td>

</ tr>

<tr>

<th>速度:</ th>

<td id =“ speed”>-</ td>

<th>时间戳:</ th>

<td id =“ timestamp”>-</ td>

</ tr>

</ table>

<script>

navigator.geolocation.getCurrentPosition(displayPosition);

函数displayPosition(pos){

var pr operties = ['经度','纬度','高度','精度','altitudeAccuracy','航向','速度']];

for(var i = 0,len = properties.length; i <len; i ++){

var value = pos.coords [properties [i]];

document.getElementById(properties [i])。innerHTML =值;

}

document.getElementById('timestamp')。innerHTML = pos.timestamp;

}

</ script>

</ body>

</ html>

返回的位置对象包含两个属性,坐标:返回坐标信息;时间戳:获取坐标信息的时间。其中坐标又包括以下属性:纬度:纬度;经度:经度;海拔:高度;精度:精确度(米); heightAccuracy:高度精确度(米);航向:行进方向;速度:行进速度(米/秒)。

并不是所有的信息都会返回,这必须您携带浏览器的设备。像有GPS,加速器,罗盘的移动设备会返回大部分信息,家用电脑就不行了。

点击允许,获取坐标信息。

2.处理异常

现在我们介绍了getCurrentPosition的异常处理,他是通过使用errorCallback进行函数实现的。函数返回的参数error包含两个属性,代码:错误类型的代码;消息:错误信息。code包含三个值: 1:用户没有授权使用地理位置; 2:无法获取坐标信息; 3:获取信息超时。

下面我们看个例子:

复制代码代码如下: 

<!DOCTYPE HTML>

<html>

<head>

<title>示例</ title>

<style>

表{边界折叠:折叠;}

th,td {填充:4px;}

{文本对齐:右;}

< / style>

</ head>

<body>

<table border =“ 1”>

<tr>

<th>经度:</ th>

<td id =“ longitude”>-</ td>

<th>纬度:</ th>

<td id =“ latitude”>-</ td>

</ tr>

<tr>

<th>海拔高度:</ th>

<td id =“ altitude”>-</ td>

<th>准确性:< / th>

<td id =“ accuracy”>-</ td>

</ tr>

<tr>

<th>高度精度:</ th>

<td id =“ altitudeAccuracy”>-</ td>

<th>标题:</ th>

<td id =“ heading”>-</ td>

</ tr>

<tr>

<th>速度:</ th>

<td id =“ speed”>-</ td>

<th>时间戳:</ th>

<td id =“ timestamp”>-</ td>

</ tr>

<tr>

<th>错误代码:</ th>

<td id =“ errcode”>-</ td>

<th>错误消息:</ th>

<td id =“ errmessage”>-</ td>

</ tr>

</ table>

<script>

navigator.geolocation.getCurrentPosition(displayPosition,handleError);

函数displayPosition(pos){

var properties = [“经度”,“纬度”,“海拔”,“

document.getElementById(properties [i])。innerHTML =值;

}

document.getElementById(“ timestamp”)。innerHTML = pos.timestamp;

}

函数handleError(err){

document.getElementById(“ errcode”)。innerHTML = err.code;

document.getElementById(“ errmessage”)。innerHTML = err.message;

}

</ script>

</ body>

</ html>

拒绝授权,运行结果:

3.使用

geoolocation可选参数项getCurrentPosition(callback,errorCallback,options)中的选项有如下参数可以使用,启用高精度:使用最好的效果;超时时间:超时时间(毫秒);最大年龄:指定缓存时间(毫秒)。我们来下下面的例子:

复制代码代码如下: 

<!DOCTYPE HTML>

<html>

<head>

<title>示例</ title>

<style>

表{边界折叠:折叠;}

th,td {填充:4px;}

{文本对齐:右;}

< / style>

</ head>

<body>

<table border =“ 1”>

<tr>

<th>经度:</ th>

<td id =“ longitude”>-</ td>

<th>纬度:</ th>

<td id =“ latitude”>-</ td>

</ tr>

<tr>

<th>海拔高度:</ th>

<td id =“ altitude”>-</ td>

<th>准确性:< / th>

<td id =“ accuracy”>-</ td>

</ tr>

<tr>

<th>高度精度:</ th>

<td id =“ altitudeAccuracy”>-</ td>

<th>标题:</ th>

<td id =“ heading”>-</ td>

</ tr>

<tr>

<th>速度:</ th>

<td id =“ speed”>-</ td>

<th>时间戳:</ th>

<td id =“ timestamp”>-</ td>

</ tr>

<tr>

<th>错误代码:</ th>

<td id =“ errcode”>-</ td>

<th>错误消息:</ th>

<td id =“ errmessage”>-</ td>

</ tr>

</ table>

<script>

var options = {

enableHighAccuracy:false,

timeout:2000,

maximumAge:30000

};

navigator.geolocation.getCurrentPosition(displayPosition,handleError,options);

var属性= [“经度”,“纬度”,“高度”,“精度”,“ altitudeAccuracy”,“航向”,“速度”];

for(var i = 0; i <properties.length; i ++){

var value = pos.coords [properties [i]];

document.getElementById(properties [i])。innerHTML =值;

}

document.getElementById(“ timestamp”)。innerHTML = pos.timestamp;

}

函数handleError(err){

document.getElementById(“ errcode”)。innerHTML = err.code;

document.getElementById(“ errmessage”)。innerHTML = err.message;

}

</ script>

</ body>

</ html>

4.监视位置变化

下面我们介绍使用watchPosition方法实现位置变化的监视,他的使用方法和getCurrentPosition一样。

复制代码代码如下: 

<!DOCTYPE HTML>

<html>

<head>

<title>示例</ title>

<style>

表{边界折叠:折叠;}

th,td {填充:4px;}

{文本对齐:右;}

< / style>

</ head>

<body>

<table border =“ 1”>

<tr>

<th>经度:</ th>

<td id =“ longitude”>-</ td>

<th>纬度:</ th>

<td id =“ latitude”>-</ td>

</ tr>

<tr>

<th>海拔高度:</ th>

<td id =“ altitude”>-</ td>

<th>准确性:< / th>

<td id =“ accuracy”>-</ td>

</ tr>

<tr>

<th>高度精度:</ th>

<td id =“ altitudeAccuracy”>-</ td>

<th>标题:</ th>

var watchID = navigator.geolocation.watchPosition(displayPosition,handleError,options); document.getElementById(“ pressme”)。onclick =函数(e){

navigator.geolocation.clearWatch(watchID);

};

函数displayPosition(pos){

var properties = [“经度”,“纬度”,“海拔”,“精度”,“ altitudeAccuracy”,“航向”,“速度”];

for(var i = 0; i <properties.length; i ++){

var value = pos.coords [properties [i]];

document.getElementById(properties [i])。innerHTML =值;

}

document.getElementById(“ timestamp”)。innerHTML = pos.timestamp;

}

函数handleError(err){

document.getElementById(“ errcode”)。innerHTML = err.code;

document.getElementById(“ errmessage”)。innerHTML = err.message;

}

</ script>

</ body>

当点击Cancel Watch按钮时,停止监视。demo

“HTML5中怎么使用地理位置实现定位功能”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. HTML5 地理位置定位(HTML5 Geolocation)
  2. 怎么使用HTML5地理位置定位功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

html5

上一篇:HTML5借助类库怎么开发

下一篇:html5中怎么为audio标签增加停止按钮

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》