发布更新对象属性

时间:2017-11-27 14:59:11

标签: jquery

lonlat值应该通过调用浏览器的地理定位服务来更新。 Console.log语句显示地理定位在if / else块中正常工作,但coords.loncoords.lat未更新。这可能是范围问题吗?想法?

$(document).ready(function () {
// object to hold user coordinates
    var coords = {
      lat: 0,
      lon: 0
    };

// retrieve and set user's latitude and longitude coordinates
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition( function (position) {
            coords.lon = position.coords.longitude;
            coords.lat = position.coords.latitude;
        });
    } else {
        alert("Sorry, I couldn't locate you. Here's the weather in Paris, France.");
        coords.lon = 48.8566;
        coords.lat = 2.3522;
    }

// enumerate AJAX request settings & pass in coordinate settings
    var ajaxOptions = {
        crossDomain:true,
        dataType:"json",
        url:"https://fcc-weather-api.glitch.me/api/current?",
        data: {
            lon:coords.lon,
            lat:coords.lat
        },
        method:"GET",
        success: function (json) {
            $("#description").html(JSON.stringify(json));
            alert(coords.lat);
        },
        error: function () {
            alert("Ajax request failed");
        }
    };

// make AJAX request by passing in options specified in object above
    $.ajax(ajaxOptions);

0 个答案:

没有答案