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

创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站设计、成都网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的玛纳斯网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
我们可以通过navigator.geolocation获取地理位置对象,他提供了下列方法:
getCurrentPosition(回调,errorCallback,选项):获取当前位置;
watchPosition(回调,错误的选项):开始监控当前位置;
clearWatch(ID):停止监控当前位置
注:下面例子使用的浏览器是铬,使用其他浏览器我不能保证运行结果和例子显示的结果一致
1.当前电子杂志位置
我们将使用getCurrentPosition方法获取当前位置,位置信息不会以结果的形式直接返回,我们需要使用回调函数进行处理。
复制代码代码如下:
<!DOCTYPE HTML>
示例 title>表{border-colla ps e:塌陷;}
th,td {padding:4px;}
th {text-align:right; }
style>
head>
| 经度: th> | - td> | 纬度: th> | - td> tr> |
|---|
| 海拔高度: th> | - td> | 准确性: th> | - td> tr> |
|---|
| 海拔精度: th> | - td> | 标题: th> | - td> tr> |
|---|
| 速度: th> | - td> | 时间戳: th> | - td> tr> table> navigator.geolocation.getCurrentPosition(displayPosition); 函数displayPosition(pos){ var pr operties = ['经度','纬度','高度','精度','altitudeAccuracy','航向','速度']]; for(var i = 0,len = properties.length; 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> 示例 title>表{边界折叠:折叠;} th,td {填充:4px;} {文本对齐:右;} < / style> head> | 经度: th> | - td> | 纬度: th> | - td> tr> |
|---|
| 海拔高度: th> | - td> | 准确性:< / th> | - td> tr> |
|---|
| 高度精度: th> | - td> | 标题: th> | - td> tr> |
|---|
| 速度: th> | - td> | 时间戳: th> | - td> tr> |
|---|
| 错误代码: th> | - td> | 错误消息: th> | - td> tr> table> 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> 示例 title>表{边界折叠:折叠;} th,td {填充:4px;} {文本对齐:右;} < / style> head> | 经度: th> | - td> | 纬度: th> | - td> tr> |
|---|
| 海拔高度: th> | - td> | 准确性:< / th> | - td> tr> |
|---|
| 高度精度: th> | - td> | 标题: th> | - td> tr> |
|---|
| 速度: th> | - td> | 时间戳: th> | - td> tr> |
|---|
| 错误代码: th> | - td> | 错误消息: th> | - td> tr> table> var options = { enableHighAccuracy:false, timeout:2000, maximumAge:30000 }; navigator.geolocation.getCurrentPosition(displayPosition,handleError,options); var属性= [“经度”,“纬度”,“高度”,“精度”,“ altitudeAccuracy”,“航向”,“速度”]; for(var i = 0; 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> 示例 title>表{边界折叠:折叠;} th,td {填充:4px;} {文本对齐:右;} < / style> head> | 经度: th> | - td> | 纬度: th> | - td> tr> |
|---|
| 海拔高度: th> | - td> | 准确性:< / th> | - td> tr> |
|---|
| 高度精度: th> | - td> | 标题: 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 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中怎么使用地理位置实现定位功能”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!
分享标题:HTML5中怎么使用地理位置实现定位功能
网站链接:http://www.tsicrk.com/article/pojhpg.html
基本
文件
流程
错误
SQL
调试
- 请求信息 : 2026-06-07 17:19:23 HTTP/1.1 GET : /article/pojhpg.html
- 运行时间 : 1.4400s ( Load:0.0063s Init:0.7742s Exec:0.6499s Template:0.0096s )
- 吞吐率 : 0.69req/s
- 内存开销 : 2,246.87 kb
- 查询信息 : 12 queries 5 writes
- 文件加载 : 36
- 缓存信息 : 0 gets 0 writes
- 配置加载 : 130
- 会话信息 : SESSION_ID=4nac0hie16iqe1b8qlcaetui77
- /www/wwwroot/tsicrk.com/index.php ( 1.09 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/ThinkPHP.php ( 4.61 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Think.class.php ( 12.26 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Storage.class.php ( 1.37 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Storage/Driver/File.class.php ( 3.52 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Mode/common.php ( 2.82 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Common/functions.php ( 53.56 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Hook.class.php ( 4.01 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/App.class.php ( 13.49 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Dispatcher.class.php ( 14.79 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Route.class.php ( 13.36 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Controller.class.php ( 11.23 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/View.class.php ( 7.59 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/BuildLiteBehavior.class.php ( 3.68 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ParseTemplateBehavior.class.php ( 3.88 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ContentReplaceBehavior.class.php ( 1.91 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Conf/convention.php ( 11.15 KB )
- /www/wwwroot/tsicrk.com/App/Common/Conf/config.php ( 2.14 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Lang/zh-cn.php ( 2.55 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Conf/debug.php ( 1.49 KB )
- /www/wwwroot/tsicrk.com/App/Home/Conf/config.php ( 0.31 KB )
- /www/wwwroot/tsicrk.com/App/Home/Common/function.php ( 3.33 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ReadHtmlCacheBehavior.class.php ( 5.62 KB )
- /www/wwwroot/tsicrk.com/App/Home/Controller/ArticleController.class.php ( 6.02 KB )
- /www/wwwroot/tsicrk.com/App/Home/Controller/CommController.class.php ( 1.60 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Model.class.php ( 60.11 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php ( 32.43 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db/Driver/Pdo.class.php ( 16.74 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Cache.class.php ( 3.83 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Cache/Driver/File.class.php ( 5.87 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Template.class.php ( 28.16 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Template/TagLib/Cx.class.php ( 22.40 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Template/TagLib.class.php ( 9.16 KB )
- /www/wwwroot/tsicrk.com/App/Runtime/Cache/Home/7540f392f42b28b481b30614275e4e55.php ( 17.71 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/WriteHtmlCacheBehavior.class.php ( 0.97 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ShowPageTraceBehavior.class.php ( 5.24 KB )
- [ app_init ] --START--
- Run Behavior\BuildLiteBehavior [ RunTime:0.000006s ]
- [ app_init ] --END-- [ RunTime:0.000031s ]
- [ app_begin ] --START--
- Run Behavior\ReadHtmlCacheBehavior [ RunTime:0.000288s ]
- [ app_begin ] --END-- [ RunTime:0.000306s ]
- [ view_parse ] --START--
- [ template_filter ] --START--
- Run Behavior\ContentReplaceBehavior [ RunTime:0.000057s ]
- [ template_filter ] --END-- [ RunTime:0.000086s ]
- Run Behavior\ParseTemplateBehavior [ RunTime:0.006606s ]
- [ view_parse ] --END-- [ RunTime:0.006637s ]
- [ view_filter ] --START--
- Run Behavior\WriteHtmlCacheBehavior [ RunTime:0.000169s ]
- [ view_filter ] --END-- [ RunTime:0.000186s ]
- [ app_end ] --START--
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`pid`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( pid= )
- [8] Undefined index: pid /www/wwwroot/tsicrk.com/App/Home/Controller/ArticleController.class.php 第 47 行.
- [8] Undefined index: db_host /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php 第 120 行.
- [8] Undefined index: db_port /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php 第 121 行.
- [8] Undefined index: db_name /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php 第 122 行.

1.4400s 
|
|---|
|
|---|
|
|---|
|
|---|