Javascript 使用传单时检查用户是否共享位置';s locate()

Javascript 使用传单时检查用户是否共享位置';s locate(),javascript,jquery,geolocation,leaflet,Javascript,Jquery,Geolocation,Leaflet,我试图在地图上设置一个按钮,允许用户缩放到他的位置。我正在使用传单映射库() locate()方法非常有效,但我想确保用户确实共享了他的位置,因为如果浏览器返回位置,我想更改按钮。以下是我一直尝试使用的代码: function locateUser() { try { this.map.locate({setView: true}); } catch(err) { alert("Couldn't find your location.");

我试图在地图上设置一个按钮,允许用户缩放到他的位置。我正在使用传单映射库()

locate()方法非常有效,但我想确保用户确实共享了他的位置,因为如果浏览器返回位置,我想更改按钮。以下是我一直尝试使用的代码:

function locateUser() {
    try {
        this.map.locate({setView: true});
    } catch(err) {
        alert("Couldn't find your location.");
        return false;
    }
    return true;
}

$("document").ready(function() {
    var map = null;
    initmap(); // This is the function that displays the map.

    // Here is where I need to check whether the user was located.
    // If he is located I want to switch the button to one that will
    // reset the view.
    var is_located = 0;
    $("#myLocation").click(function() {
        if (is_located == 0) {
            if (locateUser()){
                is_located = 1;
                $(this).attr('value', 'WILMINGTON');
            };
        } else if (is_located == 1) {
            $(this).attr('value', 'MY LOCATION');
            is_located = 0;
        };
    });
});
传单文档说locate()方法返回locationfound事件或locationerror事件,但当我尝试记录locate()返回的内容时,我只得到一个对象,我不清楚它是否找到了用户的位置。

传单的locate()方法将只返回Map对象,而不返回geolocation调用的状态。然而,传单将在尝试地理定位后引发两个事件之一:

  • 地点发现
  • 定位误差
我已在以下网站上载了使用这些事件的示例:

您可以看到,我已经注册了每个事件,以确定地理位置调用是否成功


希望有帮助。

太好了。我把东西换成了
$(this.attr('value','WILMINGTON')
进入('locationfound'…上的
…的回调函数,它工作得很好。我会在清理完后发布结束代码。非常感谢您的帮助。