I am currently working on obtaining address values from geolocation in my JavaScript code. Here is the script I have so far:
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.watchPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by your browser.";
}
}
function showPosition(position) {
var lat = position.coords.latitude;
var lag = position.coords.longitude;
$("#latit").val(lat);
$("#lang").val(lag);
var settings = {
"async": true,
"crossDomain": true,
"url": "https://us1.locationiq.com/v1/reverse.php?key=//////////&lat="+lat+"&lon="+lag+"&format=json",
"method": "POST"
}
$.ajax(settings).done(function (response) {
console.log(response);
});
}
After making the API call, I receive a JSON response with address information. Here is a sample response:
{place_id: "192741603", licence: "https://locationiq.com/attribution", osm_type: "way", osm_id: "548559489", lat: "32.2814427", …}
address:
city: "קדימה - צורן"
country: "ישראל"
country_code: "il"
postcode: "NO"
state: "מחוז המרכז"
suburb: "שיכון יציב"
__proto__: Object
boundingbox: (4) ["32.2808557", "32.2814427", "34.9092769", "34.9114099"]
display_name: "שיכון יציב, קדימה - צורן, מחוז המרכז, NO, ישראל"
lat: "32.2814427"
licence: "https://locationiq.com/attribution"
lon: "34.9094007"
osm_id: "548559489"
osm_type: "way"
place_id: "192741603"
__proto__: Object
I am now looking for a way to extract and store the address values from the response into JavaScript variables. Can someone help me with this?