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

Checking whether user shared location when using Leaflet's locate()

本文关键字:locate 位置 共享 用户 是否 单时 检查      更新时间:2024-01-10

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

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()方法返回一个locationfind事件或一个locationerror事件,但当我试图记录locate(()返回的内容时,我只得到一个对象,我不清楚它是否找到了用户的位置。

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

  • 已找到位置
  • 位置错误

我上传了一个使用这些事件的示例,网址是:http://jsfiddle.net/XwbsU/5/

您可以看到,我已经注册了这些事件中的每一个,以确定地理定位调用是否成功。

希望能有所帮助。