加入收藏 | 设为首页 | 会员中心 | 我要投稿 焦作站长网 (https://www.0391zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

如何在网站上添加谷歌定位信息

发布时间:2020-03-13 21:14:57 所属栏目:MySql教程 来源:站长网
导读:如何在网站上添加谷歌定位信息?下面小编就为大家带来一篇在网站上添加谷歌定位信息的实现方法。希望对大家有所帮助。一起跟随小编过来看看吧

我们可以很容易地使用HTML5导航对象获取当前位置。请按照以下步骤来获得城市/国家的细节。

首先包括jQuery库

JavaScript Code复制内容到剪贴板

<script type="text/javascript" src="jquery.js"></script>   

  

jQuery(function() {   

  

  //call navigator object to get latitude and logtitute   

  

  if(navigator.geolocation) {   

  

//getting current latitude and longtitude   

  

navigator.geolocation.getCurrentPosition(function(position) {   

  

  //get location details based on latitude and longtitude from Google   

  

  var jsonUrl = "https://maps.googleapis.com/maps/api/geocode/json?latlng="+position.coords.latitude+","+position.coords.longitude   

  

  jQuery.getJSON(jsonUrl , function( data ) {   

  

     var results = data.results[0];   

  

              console.info(results); //check firebug   

  

     alert(results.address_components[5].long_name); //City   

  

     alert(results.address_components[6].long_name); //State   

  

     alert(results.address_components[7].long_name); //Country   

  

  },   

  

  function(){   

  

     alert('Failed to get Location Details...');   

  

  });   

  

return false;   

  

    

  

       });   

  

  }});  

以上这篇如何在网站上添加谷歌定位信息就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

(编辑:焦作站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读