首頁 > web前端 > H5教程 > 主體

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

巴扎黑
發布: 2017-05-21 16:38:23
原創
2645 人瀏覽過

如何在網站上新增Google定位資訊?下面小編就為大家帶來一篇在網站上加入Google定位資訊的實作方法。希望對大家有幫助。一起跟著小編過來看看吧

我們可以很容易地使用HTML5導航物件來取得目前位置。請按照以下步驟來獲得城市/國家的細節。

首先包含jQuery函式庫

#JavaScript Code複製內容到剪貼簿

  1. <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(&#39;Failed to get Location Details...&#39;);   
      });   
    return false;   
           });   
      }});
    登入後複製
#

以上是如何在網站上添加谷歌定位信息的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!