关于广皓图文

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

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> 文章路径:<a href="http://wzjierui.cn/article/pojhpg.html">http://wzjierui.cn/article/pojhpg.html</a> </div> </div> <div class="other"> <h3>其他资讯</h3> <ul> <li> <a href="/article/sdepep.html">什么是cn域名?cn域名与com域名区别在哪?</a> </li><li> <a href="/article/sdepsp.html">io域名和com分别是怎么样的域名呢?io域名怎么进行注册?</a> </li><li> <a href="/article/sdejis.html">免费的海外vps服务器具备哪些优势?</a> </li><li> <a href="/article/sdepdc.html">怎么申请空间和域名?怎样才能申请到合适的空间和域名?</a> </li><li> <a href="/article/sdejhi.html">浅谈数据分析和数据建模</a> </li> </ul> </div> </div> <div class="line"></div> <!--底部--> <footer id="5"> <div class="foot1 container"> <div class="list"> <div class="item"> <a href="javascript:;"> <span class="ico1"><i class="iconfont"></i><img src="/Public/Home/img/ewm.png" alt=""></span> <strong>关注我们</strong> </a> </div> <div class="item"> <a href="" target="_blank"> <span><i class="iconfont"></i></span> <strong>索要报价</strong> </a> </div> <div class="item"> <a href="" target="_blank"> <span><i class="iconfont"></i></span> <strong>我要咨询</strong> </a> </div> <div class="item"> <a href="" target="_blank"> <span><i class="iconfont"></i></span> <strong>找到我们</strong> </a> </div> <div class="item"> <a href="" target="_blank"> <span><i class="iconfont"></i></span> <strong>投诉建议</strong> </a> </div> </div> <div class="tel"> <dl> <tel><a href="tel:400-028-6601" target="_blank">400-028-6601</a></tel><br> <span>也许您需要专业的服务,欢迎来电咨询</span> </dl> <dl> <tel><a href="tel:18980820575" target="_blank">18980820575</a></tel><br> <span>您的需求,是我们前进的动力</span> </dl> </div> </div> <div class="friend"> <div class="container"> <span class="tit">友情链接:</span> <div class="inner"> <a href="https://www.cdcxhl.com/wangzhandingzhi.html" target="_blank">成都企业网站定制</a><a href="https://www.cdcxhl.com/xiaochengx.html" target="_blank">成都小程序开发公司</a><a href="https://www.cdcxhl.com/zuyong/" target="_blank">成都服务器租用</a><a href="https://www.cdcxhl.com/jigui/" target="_blank">服务器机柜租用</a><a href="https://www.cdcxhl.com/xiaochengx.html" target="_blank">微信小程序开发公司</a><a href="https://www.cdcxhl.com/yunying.html" target="_blank">网站托管运营</a><a href="https://www.cdcxhl.com/waimao.html" target="_blank">成都外贸网站建设</a><a href="https://www.cdcxhl.com/google.html" target="_blank">谷歌推广公司</a><a href="https://www.cdcxhl.com/yingxiao.html" target="_blank">营销网站建设</a><a href="https://www.cdcxhl.com/xiaochengx.html" target="_blank">小程序开发公司</a><a href="https://www.cdcxhl.com/ruanwen/" target="_blank">软文营销</a><a href="https://www.cdcxhl.com/" target="_blank">成都网站制作公司</a><a href="https://www.cdcxhl.com/yunying.html" target="_blank">成都网站托管运营</a><a href="https://www.cdcxhl.com/tuoguan/" target="_blank">IDC机房托管</a><a href="https://www.cdcxhl.com/xiaochengx.html" target="_blank">成都小程序开发</a><a href="https://www.cdcxhl.com/link/" target="_blank">友情链接</a><a href="https://www.cdcxhl.com/ruanwen/" target="_blank">软文发布</a><a href="https://www.cdcxhl.com/xiangyingshi.html" target="_blank">成都响应式网站建设</a> </div> </div> </div> <div class="foot"> <div class="container"> <div class="footNav"> <h3>网站建设</h3> <a href="http://www.cdkjz.cn/fangan/zhongbiao/" target="_blank">奢侈品网站建设方案</a><a href="http://chengdu.cdcxhl.com/dingzhi/" target="_blank">定制网站建设</a><a href="http://www.cxhlcq.com/qiye/" target="_blank">重庆企业网站建设</a> </div> <div class="footNav"> <h3>服务器托管</h3> <a href="http://www.cqfuwuqi.com/" target="_blank">重庆服务器托管</a><a href="https://www.cdcxhl.com/idc/cqhx.html" target="_blank">重庆电信回兴机房托管</a><a href="https://www.cdcxhl.com/idc/ziyang.html" target="_blank">资阳天府云计算中心</a> </div> <div class="footNav"> <h3>网站制作</h3> <a href="http://chengdu.xwcx.net/mobile/" target="_blank">移动手机网站制作</a><a href="http://m.cdcxhl.com/" target="_blank">成都网站制作</a><a href="http://chengdu.cdxwcx.cn/wangzhan/" target="_blank">手机网站制作</a> </div> <div class="footNav"> <h3>企业服务</h3> <a href="https://www.cdcxhl.com/service/licence.html" target="_blank">药房许可证</a><a href="https://www.cdcxhl.com/hangyead/" target="_blank">一元广告</a><a href="https://www.cdcxhl.com/shoulu/" target="_blank">网站收录</a> </div> <div class="fr ecode"> <div class="fl"> <img src="/Public/Home/img/ewm.jpg"> <p>关注企业微信</p> </div> <div class="fr slogan"> <p class="icon"> <a class="ph" href=""><i class="iconfont"></i></a> <a class="qq" href="tencent://message/?uin=1683211881&Site=&Menu=yes"><i class="iconfont"></i></a> </p> <p> <i>想要找 </i> <a href="">小程序开发</a>、<a href="">APP开发</a>、 <a href="">营销型网站建设</a>、<a href="">网站建设</a>、 <i><a href="">网站定制开发</a></i> ,就选<a href="">广皓图文</a> </p> </div> </div> </div> <div class="bottom container"> <p class="fl"> 版权所有:青羊区广皓图文设计工作室(个体工商户) 备案号:<a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">蜀ICP备2025118593号</a> 服务热线:028-86922220 </p> <p class="fr"> <a href="https://www.cdxwcx.com/" target="_blank">成都网站建设</a>: <a href="https://www.cdcxhl.com/" target="_blank">创新互联</a> </p> </div> </div> </footer> <!--在线咨询--> <div class="fot"> <ul> <li> <a href="" target="_blank"> <img src="/Public/Home/img/fot1.png" alt=""> <p>在线咨询</p> </a> </li> <li> <a href="tel:18980820575" target="_blank"> <img src="/Public/Home/img/fot2.png" alt="联系温江建站"> <p>拨打电话</p> </a> </li> </ul> </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>