【问题标题】:jQuery Geolocation : Cannot read property 'coords' of undefinedjQuery Geolocation:无法读取未定义的属性“坐标”
【发布时间】:2017-09-08 21:53:45
【问题描述】:

我已经浏览了几个小时来寻找解决方案。 我对JavaScript的了解不多,其实我正在学习它。

在获取用户的 GPS 位置时遇到问题。我想 : - 获取用户的位置(jQuery)。 - 通过 Ajax > PHP 将其传输到服务器端。

这是我的 JS 代码:

$(document).ready(function () {
    initCoords();
});

function initCoords() {
    if (navigator.geolocation)
    {
        navigator.geolocation.getCurrentPosition(updateLocation, errorHandler, {enableHighAccuracy: false, maximumAge: 60000, timeout: 27000});
    } else
    {
        alert('Wrong browser.');
    }
}

function updateLocation(position)
{
    var longitude=position.coords.longitude;
    var latitude=position.coords.latitude;
    console.log(longitude);
    $.ajax({
        url: "../../ajax/account/handlegeolocation",
        type: "post",
        dataType: "json",
        data: {"longitude": longitude, "latitude": latitude},
        success: function (response) {
            console.log(response.message);
        },
        error: function (xmlhttprequest, textstatus, message) {
            console.log(message);
        }
    }).then(function () {
        setTimeout(updateLocation, 30);
    });
}

function errorHandler(error)
{
    console.log('Geolocation error : code ' + error.code + ' - ' + error.message);
}

令我惊讶的是加载页面时看到的内容。 位置返回了,但是JS说返回位置的对象是未定义的。

感谢您的帮助!

【问题讨论】:

    标签: javascript jquery geolocation


    【解决方案1】:

    这是因为您没有从

    传递位置对象
    setTimeout(updateLocation, 30);
    

    从超时调用函数initCoords 将解决您的问题。

    或者这是您的代码,经过一些编辑并且没有任何错误。

    $(document).ready(function () {
        initCoords();
    });
    
    function initCoords() {
        if (navigator.geolocation) {
            getGeoLocation();
        } else {
            alert('Wrong browser.');
        }
    }
    function getGeoLocation() {
        navigator.geolocation.getCurrentPosition(updateLocation, errorHandler, { enableHighAccuracy: false, maximumAge: 60000, timeout: 27000 });
    }
    function updateLocation(position) {
        var longitude = position.coords.longitude;
        var latitude = position.coords.latitude;
        $.ajax({
            url: "../../ajax/account/handlegeolocation",
            type: "post",
            dataType: "json",
            data: { "longitude": longitude, "latitude": latitude },
            success: function (response) {
                console.log(response.message);
            },
            error: function (xmlhttprequest, textstatus, message) {
                console.log(message);
            }
        }).then(function () {
            setTimeout(getGeoLocation, 30);
        });
    }
    
    function errorHandler(error) {
        console.log('Geolocation error : code ' + error.code + ' - ' + error.message);
    }
    

    【讨论】:

    • 谢谢!它正在工作,但在页面加载时不传输数据。这意味着只有在达到第一个超时时才传输 POST 数据。然后,一切正常。你有什么想法吗?
    • JavaScript 没有错。如果您遇到问题,那么它必须在服务器端。 30 毫秒的更新时间也太短了。
    猜你喜欢
    • 2021-11-26
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    相关资源
    最近更新 更多