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