There is a ready code, it displays the coordinates of your location, but you still need to display the city and country!

var findMeButton = $('.find-me'); // Check if the browser has support for the Geolocation API if (!navigator.geolocation) { findMeButton.addClass("disabled"); $('.no-browser-support').addClass("visible"); } else { findMeButton.on('click', function(e) { e.preventDefault(); navigator.geolocation.getCurrentPosition(function(position) { // Get the coordinates of the current possition. var lat = position.coords.latitude; var lng = position.coords.longitude; $('.latitude').text(lat.toFixed(3)); $('.longitude').text(lng.toFixed(3)); $('.coordinates').addClass('visible'); // Create a new map and place a marker at the device location. var map = new GMaps({ el: '#map', lat: lat, lng: lng }); map.addMarker({ lat: lat, lng: lng }); }); }); } 

https://jsfiddle.net/dannymarkov/ubrvm4ao/ click on the blue button!

  • one
    Look at this answer: www.stackoverflow.com/a/544759/186083 However, there is a possibility that in different coordinates the format of the returned data differs in the number of returned items. - Visman
  • @Visman is what you need !!!! thanks - kursof

1 answer 1

via google geocode you can get:

  var geocoder = new google.maps.Geocoder(); geocoder.geocode({address: lat + ',' + lng}, function (results, status) { if (status !== google.maps.GeocoderStatus.OK || !results[0]) { return; } var result = results[0]; var city, region, country; for (var i = 0; i < result.address_components.length; i++) { if (result.address_components[i].types[0] === "locality") { city = result.address_components[i]; } if (result.address_components[i].types[0] === "administrative_area_level_1") { region = result.address_components[i]; } if (result.address_components[i].types[0] === "country") { country = result.address_components[i]; } } alert(city.long_name + ", " + region.long_name + ", " + country.short_name) console.log(results); }); 

https://jsfiddle.net/ubrvm4ao/298