-3

我有这个用于我的地理位置的代码,我正在尝试获取地址值并将它们存储到 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);
    });

}

这是我从控制台中的 JSON 响应中得到的结果:

{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

如何从地址中获取值并将它们存储在 javascript var 中?

4

1 回答 1

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

确保您知道如何处理异步函数

于 2019-03-10T20:50:03.783 回答