本篇内容介绍了“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://www.scjierui.cn/article/pojhpg.html">http://www.scjierui.cn/article/pojhpg.html</a>
</div>
</div>
<div class="other">
<h3>其他资讯</h3>
<ul>
<li>
<a href="/article/jpejgo.html">HibernateSession的delete()方法怎么用</a>
</li><li>
<a href="/article/jpejdo.html">equals与==的区别有哪些</a>
</li><li>
<a href="/article/jpejej.html">SQLServer和MySQL的区别是什么</a>
</li><li>
<a href="/article/jpejso.html">css中的transform-origin属性怎么用</a>
</li><li>
<a href="/article/jpejse.html">怎么解决mysql"Accessdeniedfotuser'root'@'localhost'"的问题</a>
</li> </ul>
</div>
</div>
</div>
<footer>
<div class="footop">
<div class="wrap">
<div class="bottomrpw">
<div class="erp arp">
<dl>
<dt>ADDRESS</dt>
<dd class="address"> <i class="icon"></i> <span class="word">成都市青羊区锦天国际1号楼1002室</span> </dd>
</dl>
</div>
<div class="erp arp">
<dl>
<dt>TEL</dt>
<dd class="phonum"> <i class="icon"></i> <span class="word en"> <a href="tel:18980820575">18980820575</a> </span> </dd>
</dl>
</div>
<div class="erp crp">
<dl>
<dt>OTHER</dt>
<dd> <a class="word get-quote">获得报价与方案</a> </dd>
<dd> <a href="#" target="_blank" rel='nofollow' class="word" title="付款方式">付款方式</a> </dd>
</dl>
</div>
<div class="erp code-rp">
<dl>
<dt>Wechat</dt>
<dd class="code-wrap"> <span class="code"> <img src="/Public/Home/images/qr-code.jpg" alt="美图齐众微信公众号" /> </span> </dd>
</dl>
</div>
</div>
</div>
</div>
<div class="footerbot">
<div class="friendlinks">
<div class="wrap">
<ul class="rpl">
<li><a href="http://www.cdhuace.com/fuwu/huace.html" title="成都画册快印" target="_blank">成都画册快印</a></li><li><a href="http://www.hzlinhua.com/" title="茶叶批发零售" target="_blank">茶叶批发零售</a></li><li><a href="http://www.cdxwcx.cn/tuoguan/sichuan.html" title="四川服务器托管" target="_blank">四川服务器托管</a></li><li><a href="http://www.njjbc.com/" title="成都企业文化墙" target="_blank">成都企业文化墙</a></li><li><a href="https://www.cdcxhl.cn/
" title="免备案空间腾讯云" target="_blank">免备案空间腾讯云</a></li><li><a href="http://www.cdanhua.cn/" title="蜜朵婚庆" target="_blank">蜜朵婚庆</a></li><li><a href="https://www.cdcxhl.cn/
" title="腾讯免备案主机" target="_blank">腾讯免备案主机</a></li><li><a href="https://www.cdxwcx.com/city/guanghan/" title="广汉做网站" target="_blank">广汉做网站</a></li><li><a href="http://www.kswcd.com/solution/" title="网站方案" target="_blank">网站方案</a></li><li><a href="http://www.ynjbc.com/" title="企业网站维护公司" target="_blank">企业网站维护公司</a></li> </ul>
</div>
</div>
<div class="wrap">
<div class="copyright"> <span class="en">©2025</span> 青羊区美图齐众设计工作室(个体工商户)四川站<span class="en">ALL RIGHTS
RESERVED.</span> <a rel="nofollow" href="https://beian.miit.gov.cn" target="_blank">蜀ICP备2025119604号-2</a> </div>
</div>
</div>
</footer>
<div class="fcwrap">
<ul class="rpl clearfix">
<li class="phone"> <a rel="nofollow" target="_blank" href="tel:18980820575"> <i class="icon"></i>
<strong>18980820575</strong> </a> </li>
<li class="qq"> <a rel="nofollow" target="_blank"
href="https://wpa.qq.com/msgrd?v=1&uin=244261566&site=qq&menu=yes"> <i class="icon"></i>
<strong>244261566</strong> </a> </li>
<li class="back-top"> <a href="javascript:void(0)" rel="nofollow" class="back-to-top"> <i class="icon"></i>
<strong>回到顶部</strong> </a> </li>
</ul>
</div>
<!--nav-->
<div class="n-Wrap">
<div class="navBar visble show">
<div class="barlogo">
<a href="/" rel="nofollow">
<img src="/Public/Home/images/logo1.png" alt="四川做网站" />
<img src="/Public/Home/images/logo2.png" alt="四川网站设计" />
</a>
</div>
<div class="bmenu">
<i class="bar-top"><span></span></i>
<i class="bar-cen"><span></span></i>
<i class="bar-bom"><span></span></i>
<i class="bar-left"><span></span></i>
<i class="bar-right"><span></span></i>
</div>
</div>
<section class="fixmenu">
<div class="close-bar">
<i class="bar-left"><span></span></i>
<i class="bar-right"><span></span></i>
</div>
<nav class="smph">
<ul>
<li class="index-hrefs on"><a href="http://www.scjierui.cn/"><font>首页</font></a></li>
<li><a href="/about/" rel="nofollow"><font>关于美图齐众</font></a></li>
<li><a href="/service/" rel="nofollow"><font>服务范围</font></a></li>
<li><a href="/case/" rel="nofollow"><font>案例展示</font></a></li>
<li><a href="/solve/" rel="nofollow"><font>解决方案</font></a></li>
<li><a href="/news/" rel="nofollow"><font>建站资讯</font></a></li>
<li><a href="/contact/" rel="nofollow"><font>联系美图齐众</font></a></li>
</ul>
<div class="pwrap">
<span class="label">服务热线</span>
<strong class="phone"><a href="tel:18980820575">18980820575</a></strong>
</div>
</nav>
</section>
</div>
<!--end nav-->
<script src="/Public/Home/js/hotcss.js"></script>
<script type="text/javascript" src="/Public/Home/js/su_new.js"></script>
</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>