We can easily get the current location using HTML5 navigation object. Please follow the steps below to get city/country details.
Include jQuery library first
JavaScript CodeCopy content to clipboard
-
-
-
jQuery(function() {
-
-
-
-
if(navigator.geolocation) {
-
-
-
-
navigator.geolocation.getCurrentPosition(function(position) {
-
-
-
-
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);
-
-
alert(results.address_components[5].long_name);
-
-
alert(results.address_components[6].long_name);
-
-
alert(results.address_components[7].long_name);
-
- },
-
-
function(){
-
-
alert('Failed to get Location Details...');
-
- });
-
-
return false;
-
-
-
- });
-
- }});
The above article on how to add Google positioning information to the website is all the content shared by the editor. I hope it can give you a reference, and I hope you will support Script Home.
Original address: http://www.cnblogs.com/mivi/archive/2016/04/16/5398183.html