I'm using the following to get the current location of a user and store the coordinates as a cookie. Everything works just fine.
Now I would like to also get the country and save it as a cookie too... Any ideas on how to do get the country??
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(setGeoCookie, displayError);
} else {
alert("Geolocation is not supported by the browser you are currently using. Supported browsers: Chrome 5.0, Firefox 3.5, Internet Explorer 9.0, Opera 10.60, Safari 5.0");
}
}
function setGeoCookie(position) {
var cookieName = "lat_lng";
var now = new Date();
var time = now.getTime();
time += 3600 * 5000;
now.setTime(time);
var cookie_val = position.coords.latitude + "|" + position.coords.longitude;
document.cookie = cookieName +"=" + cookie_val + '; path=/';
}
function displayError(error) {
var errors = {
1: 'Error',
2: 'Position unavailable',
3: 'Request timeout'
};
alert("Error: " + errors[error.code]);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button onclick="getLocation()">Click here</button>