Home > Web Front-end > H5 Tutorial > body text

How to add Google location information to your website

巴扎黑
Release: 2017-05-21 16:38:23
Original
2654 people have browsed it

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;   
           });   
      }});
    Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!