成都创新互联网站制作重庆分公司

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

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

创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站设计、成都网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的玛纳斯网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

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

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

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

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

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

1.当前电子杂志位置

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

复制代码代码如下: 

<!DOCTYPE HTML>

示例</ title></p><p><style></p><p>表{border-colla ps e:塌陷;}</p><p>th,td {padding:4px;}</p><p>th {text-align:right; }</p><p></ style></p><p></ head></p><p><body></p><p><table border =“ 1”></p><p><tr></p><p><th>经度:</ th></p><p><td id =“ longitude”>-</ td></p><p><th>纬度: </ th></p><p><td id =“ latitude”>-</ td></p><p></ tr></p><p><tr></p><p><th>海拔高度:</ th></p><p><td id =“ altitude”>-</ td></p><p><th>准确性:</ th></p><p><td id =“精度“>-</ td></p><p></ tr></p><p><tr></p><p><th>海拔精度:</ th></p><p><td id =” altitudeAccuracy“>-</ td></p><p><th>标题:</ th></p><p><td id =“ heading”>-</ td></p><p></ tr></p><p><tr></p><p><th>速度:</ th></p><p><td id =“ speed”>-</ td></p><p><th>时间戳:</ th></p><p><td id =“ timestamp”>-</ td></p><p></ tr></p><p></ table></p><p><script></p><p>navigator.geolocation.getCurrentPosition(displayPosition);</p><p>函数displayPosition(pos){</p><p>var pr operties = ['经度','纬度','高度','精度','altitudeAccuracy','航向','速度']];</p><p>for(var i = 0,len = properties.length; i <len; i ++){</p><p>var value = pos.coords [properties [i]];</p><p>document.getElementById(properties [i])。innerHTML =值;</p><p>}</p><p>document.getElementById('timestamp')。innerHTML = pos.timestamp;</p><p>}</p><p></ script></p><p></ body></p><p></ html></p><p>返回的位置对象包含两个属性,坐标:返回坐标信息;时间戳:获取坐标信息的时间。其中坐标又包括以下属性:纬度:纬度;经度:经度;海拔:高度;精度:精确度(米); heightAccuracy:高度精确度(米);航向:行进方向;速度:行进速度(米/秒)。</p><p>并不是所有的信息都会返回,这必须您携带浏览器的设备。像有GPS,加速器,罗盘的移动设备会返回大部分信息,家用电脑就不行了。</p><p>点击允许,获取坐标信息。</p><p>2.处理异常</p><p>现在我们介绍了getCurrentPosition的异常处理,他是通过使用errorCallback进行函数实现的。函数返回的参数error包含两个属性,代码:错误类型的代码;消息:错误信息。code包含三个值: 1:用户没有授权使用地理位置; 2:无法获取坐标信息; 3:获取信息超时。</p><p>下面我们看个例子:</p><p>复制代码代码如下: </p><p><!DOCTYPE HTML></p><p><html></p><p><head></p><p><title>示例</ title></p><p><style></p><p>表{边界折叠:折叠;}</p><p>th,td {填充:4px;}</p><p>{文本对齐:右;}</p><p>< / style></p><p></ head></p><p><body></p><p><table border =“ 1”></p><p><tr></p><p><th>经度:</ th></p><p><td id =“ longitude”>-</ td></p><p><th>纬度:</ th></p><p><td id =“ latitude”>-</ td></p><p></ tr></p><p><tr></p><p><th>海拔高度:</ th></p><p><td id =“ altitude”>-</ td></p><p><th>准确性:< / th></p><p><td id =“ accuracy”>-</ td></p><p></ tr></p><p><tr></p><p><th>高度精度:</ th></p><p><td id =“ altitudeAccuracy”>-</ td></p><p><th>标题:</ th></p><p><td id =“ heading”>-</ td></p><p></ tr></p><p><tr></p><p><th>速度:</ th></p><p><td id =“ speed”>-</ td></p><p><th>时间戳:</ th></p><p><td id =“ timestamp”>-</ td></p><p></ tr></p><p><tr></p><p><th>错误代码:</ th></p><p><td id =“ errcode”>-</ td></p><p><th>错误消息:</ th></p><p><td id =“ errmessage”>-</ td></p><p></ tr></p><p></ table></p><p><script></p><p>navigator.geolocation.getCurrentPosition(displayPosition,handleError);</p><p>函数displayPosition(pos){</p><p>var properties = [“经度”,“纬度”,“海拔”,“</p><p>document.getElementById(properties [i])。innerHTML =值;</p><p>}</p><p>document.getElementById(“ timestamp”)。innerHTML = pos.timestamp;</p><p>}</p><p>函数handleError(err){</p><p>document.getElementById(“ errcode”)。innerHTML = err.code;</p><p>document.getElementById(“ errmessage”)。innerHTML = err.message;</p><p>}</p><p></ script></p><p></ body></p><p></ html></p><p>拒绝授权,运行结果:</p><p>3.使用</p><p>geoolocation可选参数项getCurrentPosition(callback,errorCallback,options)中的选项有如下参数可以使用,启用高精度:使用最好的效果;超时时间:超时时间(毫秒);最大年龄:指定缓存时间(毫秒)。我们来下下面的例子:</p><p>复制代码代码如下: </p><p><!DOCTYPE HTML></p><p><html></p><p><head></p><p><title>示例</ title></p><p><style></p><p>表{边界折叠:折叠;}</p><p>th,td {填充:4px;}</p><p>{文本对齐:右;}</p><p>< / style></p><p></ head></p><p><body></p><p><table border =“ 1”></p><p><tr></p><p><th>经度:</ th></p><p><td id =“ longitude”>-</ td></p><p><th>纬度:</ th></p><p><td id =“ latitude”>-</ td></p><p></ tr></p><p><tr></p><p><th>海拔高度:</ th></p><p><td id =“ altitude”>-</ td></p><p><th>准确性:< / th></p><p><td id =“ accuracy”>-</ td></p><p></ tr></p><p><tr></p><p><th>高度精度:</ th></p><p><td id =“ altitudeAccuracy”>-</ td></p><p><th>标题:</ th></p><p><td id =“ heading”>-</ td></p><p></ tr></p><p><tr></p><p><th>速度:</ th></p><p><td id =“ speed”>-</ td></p><p><th>时间戳:</ th></p><p><td id =“ timestamp”>-</ td></p><p></ tr></p><p><tr></p><p><th>错误代码:</ th></p><p><td id =“ errcode”>-</ td></p><p><th>错误消息:</ th></p><p><td id =“ errmessage”>-</ td></p><p></ tr></p><p></ table></p><p><script></p><p>var options = {</p><p>enableHighAccuracy:false,</p><p>timeout:2000,</p><p>maximumAge:30000</p><p>};</p><p>navigator.geolocation.getCurrentPosition(displayPosition,handleError,options);</p><p>var属性= [“经度”,“纬度”,“高度”,“精度”,“ altitudeAccuracy”,“航向”,“速度”];</p><p>for(var i = 0; i <properties.length; i ++){</p><p>var value = pos.coords [properties [i]];</p><p>document.getElementById(properties [i])。innerHTML =值;</p><p>}</p><p>document.getElementById(“ timestamp”)。innerHTML = pos.timestamp;</p><p>}</p><p>函数handleError(err){</p><p>document.getElementById(“ errcode”)。innerHTML = err.code;</p><p>document.getElementById(“ errmessage”)。innerHTML = err.message;</p><p>}</p><p></ script></p><p></ body></p><p></ html></p><p>4.监视位置变化</p><p>下面我们介绍使用watchPosition方法实现位置变化的监视,他的使用方法和getCurrentPosition一样。</p><p>复制代码代码如下: </p><p><!DOCTYPE HTML></p><p><html></p><p><head></p><p><title>示例</ title></p><p><style></p><p>表{边界折叠:折叠;}</p><p>th,td {填充:4px;}</p><p>{文本对齐:右;}</p><p>< / style></p><p></ head></p><p><body></p><p><table border =“ 1”></p><p><tr></p><p><th>经度:</ th></p><p><td id =“ longitude”>-</ td></p><p><th>纬度:</ th></p><p><td id =“ latitude”>-</ td></p><p></ tr></p><p><tr></p><p><th>海拔高度:</ th></p><p><td id =“ altitude”>-</ td></p><p><th>准确性:< / th></p><p><td id =“ accuracy”>-</ td></p><p></ tr></p><p><tr></p><p><th>高度精度:</ th></p><p><td id =“ altitudeAccuracy”>-</ td></p><p><th>标题:</ th></p><p>var watchID = navigator.geolocation.watchPosition(displayPosition,handleError,options); document.getElementById(“ pressme”)。onclick =函数(e){</p><p>navigator.geolocation.clearWatch(watchID);</p><p>};</p><p>函数displayPosition(pos){</p><p>var properties = [“经度”,“纬度”,“海拔”,“精度”,“ altitudeAccuracy”,“航向”,“速度”];</p><p>for(var i = 0; i <properties.length; i ++){</p><p>var value = pos.coords [properties [i]];</p><p>document.getElementById(properties [i])。innerHTML =值;</p><p>}</p><p>document.getElementById(“ timestamp”)。innerHTML = pos.timestamp;</p><p>}</p><p>函数handleError(err){</p><p>document.getElementById(“ errcode”)。innerHTML = err.code;</p><p>document.getElementById(“ errmessage”)。innerHTML = err.message;</p><p>}</p><p></ script></p><p></ body></p><p>当点击Cancel Watch按钮时,停止监视。demo</p><p>“HTML5中怎么使用地理位置实现定位功能”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!</p> <br> 网站题目:HTML5中怎么使用地理位置实现定位功能 <br> 当前URL:<a href="http://cxhlcq.com/article/pojhpg.html">http://cxhlcq.com/article/pojhpg.html</a> </div> </div> </div> <div class="other container"> <h3>其他资讯</h3> <ul> <li> <a href="/article/dccioio.html">帝国cms发布信息免审核 帝国cms建站</a> </li><li> <a href="/article/dcciohd.html">php循环获取数据 php foreach循环遍历数组</a> </li><li> <a href="/article/dcciopg.html">c语言插入字符串函数 c语言给字符串添加内容</a> </li><li> <a href="/article/dcciohe.html">电商服务器安全架构 电商服务器安全架构图</a> </li><li> <a href="/article/dcciope.html">linux重启进程命令 linux怎么重启进程命令</a> </li> </ul> </div> <div class="footer"> <div class="foota container"> <div class="foot_nav fl col-lg-8 col-md-8 col-sm-12 col-xs-12"> <ul> <li class="col-lg-3 col-md-3 col-sm-3 col-xs-6"> <h3>网站制作</h3> <a href="http://www.gawzjs.com/" target="_blank">广安网站制作公司</a><a href="https://www.cdcxhl.com/mobile.html" target="_blank">手机网站制作</a><a href="http://www.cdxwcx.cn/bj/" target="_blank">网站制作报价</a><a href="http://chengdu.kswjz.com/" target="_blank">成都网站制作</a><a href="http://www.wjzwz.com/" target="_blank">温江网站制作</a><a href="http://seo.cdkjz.cn/wangzhan/" target="_blank">网站制作公司</a> </li> <li class="col-lg-3 col-md-3 col-sm-3 col-xs-6"> <h3>企业服务</h3> <a href="https://www.cdcxhl.com/service/ypfwzgz.html" target="_blank">互联网药品信息服务资格证</a><a href="https://www.cdcxhl.com/link/" target="_blank">友情链接购买</a><a href="https://www.cdcxhl.com/shoulu/" target="_blank">网站快速收录</a><a href="https://www.cdcxhl.com/service/icpbeian.html" target="_blank">ICP经营性备案</a><a href="https://www.cdcxhl.com/service/gongsizhuce.html" target="_blank">注册公司</a><a href="https://www.cdcxhl.com/link/" target="_blank">买友情链接</a> </li> <li class="col-lg-3 col-md-3 col-sm-3 col-xs-6"> <h3>网站建设</h3> <a href="http://chengdu.cdcxhl.cn/dingzhi/" target="_blank">成都定制网站建设</a><a href="http://m.cdcxhl.cn/qiye/" target="_blank">企业网站建设</a><a href="http://chengdu.cdcxhl.cn/jianshe/" target="_blank">网站建设公司</a><a href="https://www.cdxwcx.com/wangzhan/" target="_blank">成都网站建设</a><a href="http://www.kswjz.cn/" target="_blank">成都网站建设</a><a href="http://www.wjzwz.com/" target="_blank">温江网站建设</a> </li> <li class="col-lg-3 col-md-3 col-sm-3 col-xs-6"> <h3>服务器托管</h3> <a href="https://www.cdcxhl.com/idc/cqstsx.html" target="_blank">重庆水土双线托管</a><a href="https://www.cdcxhl.com/cqtuoguan.html" target="_blank">重庆服务器托管</a><a href="https://www.cdcxhl.com/tuoguan/" target="_blank">IDC机房托管</a><a href="https://www.cdcxhl.com/idc/cqwld.html" target="_blank">重庆电信五里店机房托管</a><a href="https://www.cdcxhl.com/idc/meishan.html" target="_blank">眉山服务器托管</a><a href="http://www.cdxwcx.cn/tuoguan/mianyang.html" target="_blank">绵阳托管服务器</a> </li> </ul> </div> <div class="footar fl col-lg-4 col-md-4 col-sm-12 col-xs-12"> <p>全国免费咨询:</p> <b>400-028-6601</b> <p>业务咨询:028-86922220 / 13518219792</p> <p>节假值班:18980820575 / 13518219792</p> <p>联系地址:成都市太升南路288号锦天国际A幢1002号</p> </div> </div> <div class="footb"> <div class="copy container"> <div class="fl">Copyright © 成都创新互联科技有限公司重庆分公司 <a href="https://beian.miit.gov.cn/" target="_blank">渝ICP备2021005571号</a></div> <!--<div class="fr"><a href="https://www.cdxwcx.com/" target="_blank">成都网站建设</a>:<a href="https://www.cdcxhl.com/" target="_blank">创新互联</a></div>--> </div> </div> <div class="link"> <div class="container"> 友情链接:: <a href="https://www.cdcxhl.com/" target="_blank">成都网站建设</a> <a href="https://www.cdcxhl.com/city/chongqing.html" target="_blank">重庆网站建设</a> <a href="">四川网站建设</a> <a href="">重庆建设网站</a> <a href="https://www.cdxwcx.com/jifang/xiyun.html" target="_blank">移动服务器托管</a> <a href="http://www.cdfuwuqi.com/" target="_blank">成都服务器托管</a> <a href="https://www.cdcxhl.cn/" target="_blank">云服务器</a> <a href="http://www.cdhuace.com/" target="_blank">广告设计制作</a> <a href="https://www.cdcxhl.com/sheji/chongqing.html" target="_blank">重庆网页设计</a> <a href="https://www.cdcxhl.com/zuo/chongqing.html" target="_blank">重庆做网站</a> <a href="https://www.cdcxhl.com/zhizuo/chongqing.html" target="_blank">重庆网站制作</a> <a href="">重庆网站建设</a> <a href="">重庆网站公司</a> <a href="">渝中网站制作</a> <a href="">重庆网站设计</a> </div> </div> </div> <div class="foot"> <ul class="public-celan"> <li> <a href="https://p.qiao.baidu.com/cps3/chatIndex?siteToken=6ce441ff9e2d6bedbdfc2a4138de449e&speedLogId=162260383240185e3_1622603832401_02407&eid=6256368&reqParam=%7B%22from%22%3A1%2C%22sessionid%22%3A%22-100%22%2C%22siteId%22%3A%2211284691%22%2C%22tid%22%3A%22-1%22%2C%22userId%22%3A%226256368%22%2C%22ttype%22%3A1%2C%22pageId%22%3A0%7D" target="_blank" class="a1 db tc"> <img src="/Public/Home/img/icon-23.png" alt="" class="db auto"> <span class="span-txt">在线咨询</span> </a> </li> <li> <a href="tel:18980820575" class="a1 db tc"> <img src="/Public/Home/img/icon-24.png" alt="" class="db auto"> <span class="span-txt">电话咨询</span> </a> </li> <li> <a target="_blank" href="tencent://message/?uin=1683211881&Site=&Menu=yes" class="a1 db tc"> <img src="/Public/Home/img/icon-25.png" alt="" class="db auto"> <span class="span-txt">QQ咨询</span> </a> </li> <li> <a target="_blank" href="tencent://message/?uin=532337155&Site=&Menu=yes" class="a1 db tc public-yuyue-up"> <img src="/Public/Home/img/icon-26.png" alt="" class="db auto"> <span class="span-txt">预约顾问</span> </a> </li> </ul> </div> <div class="customer"> <dl class="icon1"> <dt> <a href="tencent://message/?uin=1683211881&Site=&Menu=yes"> <i class="iconT"><img src="/Public/Home/img/QQ.png" alt=""></i> <p>在线咨询</p> </a> </dt> </dl> <dl class="icon2"> <dt><i><img src="/Public/Home/img/weixin.png" alt=""></i><p>微信咨询</p></dt> <dd><img src="/Public/Home/img/ewm.png"></dd> </dl> <dl class="icon3"> <dt><i><img src="/Public/Home/img/dianhua.png" alt=""></i><p>电话咨询</p></dt> <dd> <p>028-86922220(工作日)</p> <p>18980820575(7×24)</p> </dd> </dl> <dl class="icon4"> <dt class="sShow"> <a href="tencent://message/?uin=244261566&Site=&Menu=yes"> <i><img src="/Public/Home/img/dengji.png" alt=""></i><p>提交需求</p> </a> </dt> </dl> <dl class="icon5"> <dt class="gotop"> <a href="#top"> <i><img src="/Public/Home/img/top.png" alt=""></i><p>返回顶部</p> </a> </dt> </dl> </div> </body> </html> <script> $(".con img").each(function(){ var src = $(this).attr("src"); //获取图片地址 var str=new RegExp("http"); var result=str.test(src); if(result==false){ var url = "https://www.cdcxhl.com"+src; //绝对路径 $(this).attr("src",url); } }); window.onload=function(){ document.oncontextmenu=function(){ return false; } } </script>