Home  >  Article  >  Web Front-end  >  How to add Google location information to your website

How to add Google location information to your website

巴扎黑
巴扎黑Original
2017-05-21 16:38:232687browse

How to add Google location information to the website? The editor below will bring you an implementation method of adding Google positioning information to the website. I hope to be helpful. Let’s follow the editor and take a look

We can easily use HTML5 navigation objects to get the current location. Please follow the steps below to get city/country details.

Include jQuery library first

JavaScript CodeCopy content to clipboard

  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;   
           });   
      }});

The above is the detailed content of How to add Google location information to your website. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn