成都网站建设设计

将想法与焦点和您一起共享

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://chengdu.cdxwcx.cn/article/pojhpg.html">http://chengdu.cdxwcx.cn/article/pojhpg.html</a> </div> </div> </div> <!--左边end--> <!--右边begin--> <div class="news_r"> <div class="news_t"><h2 class="h2">其他资讯</h2></div> <div class="news_ul3"> <ul> <li> <a href="/article/isjioe.html"> <h3 class="h3">for循环存在多个循环条件表达式时,循环条件的判断</h3> </a> </li><li> <a href="/article/isjsij.html"> <h3 class="h3">​xss发生的位置指的是什么</h3> </a> </li><li> <a href="/article/isjiog.html"> <h3 class="h3">怎么在易语言中修改日期框显示附件类型</h3> </a> </li><li> <a href="/article/isjepi.html"> <h3 class="h3">Java多线程中线程状态原理的示例分析</h3> </a> </li><li> <a href="/article/isjepg.html"> <h3 class="h3">nodeExpress的中间件举例分析</h3> </a> </li> </ul> </div> </div> <!--右边end--> <div class="c_l"></div> </div> </div> <!--正文end--> <!--尾部begin--> <!--尾部begin--> <footer> <div class="f_bg"> <div class="wrap"> <div class="links"> <h2 class="h2">甜橘子解决方案<a href="/solution/" title="更多" class="more">更多+</a></h2> <ul> <li><a href="/solution/xiaochengxu.html" title="小程序定制解决方案">小程序定制解决方案</a></li> <li><a href="/solution/qiyewz.html" title="企业网站建设解决方案">企业网站建设解决方案</a></li> <li><a href="/solution/menhuwz.html" title="行业门户网站建设解决方案">行业门户网站建设解决方案</a></li> <li><a href="/solution/yingxiaowz.html" title="营销型网站建设解决方案">营销型网站建设解决方案</a></li> <li><a href="/solution/waimaowz.html" title="外贸网站建设解决方案">外贸网站建设解决方案</a></li> <li><a href="/solution/pingpaiwz.html" title="品牌形象网站建设解决方案">品牌形象网站建设解决方案</a></li> <li><a href="/solution/dianziwz.html" title="数码、电子产品网站建设解决方案">数码、电子产品网站建设解决方案</a></li> <li><a href="/solution/jituanwz.html" title="集团、上市企业网站建设解决方案">集团、上市企业网站建设解决方案</a></li> <li><a href="/solution/dichanwz.html" title="房地产、地产项目网站建设解决方案">房地产、地产项目网站建设解决方案</a></li> <li><a href="/solution/zhubaowz.html" title="珠宝高端奢侈品网站建设解决方案">珠宝高端奢侈品网站建设解决方案</a></li> </ul> </div> <div class="links w2"> <h2 class="h2">我们的实力<a href="/about/" title="更多" class="more">更多+</a></h2> <ul> <li>10年专业互联网服务经验</li> <li>成都高端建站设计团队</li> <li>资深行业分析策划</li> <li>B2C营销型网站建设者</li> <li>前沿视觉设计、研发能力</li> <li>前端代码深度符合SEO优化</li> <li>成都市高新技术企业证书</li> <li>具有完备的项目管理</li> <li>完善的售后服务体系</li> <li>深厚的网络运营经验</li> <li>时刻新技术研发能力</li> <li>16个网站系统软件著作权</li> </ul> </div> <div class="f_div2_r"> <h2 class="h2">关于甜橘子<a href="/about/" title="更多" class="more">更多+</a></h2> 甜橘子网站设计,为客户量身定制各类网站建设业务,包括企业型、电子商务型、行业门户型、品牌建立型等各类网站,实战经验丰富,成功案例众多。以客户利益为出发点,甜橘子网站制作为客户规划、定制符合企业需求、带有营销价值的建站方案,提供从网站前期定位分析策划到网站界面设计... </div> <div class="c_l"></div> </div> <div class="wrap"> <div class="f_div3"> <span class="l">成都网站制作案例©2020 甜橘子设计 版权所有 | <a href="http://chengdu.cdxwcx.cn" target="_blank">甜橘子网站设计</a><a href="http://chengdu.cdxwcx.cn" target="_blank">chengdu.cdxwcx.cn</a></span> <span class="r"><a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">蜀ICP备11025516号</a></span> </div> </div> </div> </footer> <!--尾部end--> <script language="javascript" src="/Public/Home/js/foot.js"></script> <!--尾部end--> <!--侧边栏begin--> <div class="side"> <ul> <li id="qqonline_xbceo"><a href="tencent://message/?uin=631063699&Site=&Menu=yes"><i class="bgs1"></i>QQ咨询</a></li> <li class="shangqiao"><a href="tencent://message/?uin=532337155&Site=&Menu=yes" title="在线咨询"> <div><i class="bgs2"></i>在线咨询</div> </a></li> <li class="sideewm"><i class="bgs3"></i>官方微信 <div class="ewBox"></div> </li> <li class="sideetel"><i class="bgs4"></i>联系电话 <div class="telBox"> <dd class="bgs1"><span>座机</span><a href="tel:028-86922220" target="_blank">028-86922220</a></dd> <dd class="bgs2"><span>手机</span><a href="tel:13518219792" target="_blank">13518219792</a></dd> </div> </li> <li class="sidetop" onClick="goTop()" id="sidetop"><i class="bgs6"></i>返回顶部</li> </ul> </div> <script type="text/javascript"> $('.sideewm').hover(function(){ $('.ewBox').stop().fadeIn(); },function(){ $('.ewBox').stop().fadeOut(); }); $('.sideetel').hover(function(){ $('.telBox').stop().fadeIn(); },function(){ $('.telBox').stop().fadeOut(); }); $(".con_id 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); } }); </script> <!-- WPA start --> <!-- WPA end --> <!--侧边栏end--> </body> </html>