-3

i have this code for my geolocation and i am trying to get the address values and store them in to javascript var

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.watchPosition(showPosition);
    } else {
        x.innerHTML = "Geolocation is not supported by this 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);
    });

}

and this is the result i get from the JSON response in my console:

{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

how can i get values from the address and store them in javascript var?

codeKracken
  • 127
  • 14
dror shalit
  • 318
  • 2
  • 12

1 Answers1

1
$.ajax(settings).done(function (response) {
    var someVariable = response.place_id;
    // And then do something with it here because this is async
});

Make sure you know how to handle async functions.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I am a little bit lost here can you please give me a code example? – dror shalit Mar 10 '19 at 20:54
  • @drorshalit — The first three quarters of this answer **is** a code example – Quentin Mar 10 '19 at 20:54
  • i am new in json so when you say do something with it that is the point I am getting lost. – dror shalit Mar 10 '19 at 20:56
  • 1
    `alert(someVariable)`. OK, there is something you can do with the string. (Which isn't JSON, and hasn't been part of the JSON since jQuery parsed the JSON into an object). – Quentin Mar 10 '19 at 20:57
  • got it Thank U! var add = response.address['city']; – dror shalit Mar 10 '19 at 21:00
  • @Quentin don't think that `jQuery` parses `JSON` (or that one would ever have to parse JSON within a JS context). it's called "object notation", because it evaluates to object, without the least parsing required. simple test: pasting JSON into the console window. – Martin Zeitler Mar 10 '19 at 22:58
  • @MartinZeitler — You are confusing JavaScript literal syntax (which is part of the source code of a JavaScript program) with JSON (which is a stand-alone data format, in this case, fetched over HTTP by XMLHttpRequest and then passed through `JSON.parse` (both done behind the scenes by the jQuery library). – Quentin Mar 11 '19 at 07:55
  • @Quentin you're correct; else there wouldn't be any `JSON.parse()` or `JSON.stringify()`. the response from the network request is indeed still a string. – Martin Zeitler Mar 11 '19 at 08:15