【问题标题】:watchPosition() fires callback only oncewatchPosition() 只触发一次回调
【发布时间】:2013-06-07 17:38:24
【问题描述】:

我正在为 MapDotNet 的 Touchgeo (http://www.mapdotnet.com/index.php/component/content/article?id=131) 中的地理围栏功能编写 watchPosition 函数。在初始加载时,一切都运行良好;刷新时,我只收到一行调试消息,表示只有一个回调,而且我手机上的 GPS 永远不会打开。这是我的 watchPosition 函数:

navigator.geolocation.watchPosition(
    function success(pos) {
        $('#debug')
            .prepend(
                $('<div></div>').text('accuracy: ' + pos.coords.accuracy)
            )
            .css({
                textAlign: 'right',
                color: 'black'
            });
        var endpoint = isc.touchgeo.dataServicesEndpoint + "Map/mapname/Features/geofence?x={x}&y={y}&role={role}"
            .replace("{x}", pos.coords.longitude)
            .replace("{y}", pos.coords.latitude)
            .replace("{role}", isc.touchgeo.authenticationMgr.getAuthorizationRecord().Role);
        $.getJSON(endpoint, function success(data) {
            $('#debug')
                .prepend(
                    $('<div></div>').text('features: ' + data.length)
                )
                .css({
                    textAlign: 'right',
                    color: 'black'
                });
            for (layer in data) {
                if (layer in geofencingRules) {
                    geofencingRules[layer](data[layer]);
                }
            }
        });
    },
    function error(error) {
        $('#debug')
            .prepend(
                $('<div></div>').text('error: ' + error.code)
            )
            .css({
                textAlign: 'right',
                color: 'black'
            });
    },
    {
        enableHighAccuracy: true,
        maximumAge: 15000,
    }
);

有什么想法吗?

【问题讨论】:

    标签: android html google-chrome geolocation gps


    【解决方案1】:

    我想通了。基本上,positionOptions 上的maximumAge 告诉watchPosition() 使用刷新页面之前的数据。因此,GPS 从未打开,watchPosition() 也没有收到数据。解决这个问题的方法是拥有

    var maximumAge = 0;
    navigator.geolocation.watchPosition(
        function success(pos) {
            maximumAge = 15000;
            $('#debug')
                .prepend(
                    $('<div></div>').text('accuracy: ' + pos.coords.accuracy)
                )
                .css({
                    textAlign: 'right',
                    color: 'black'
                });
            var endpoint = isc.touchgeo.dataServicesEndpoint + "Map/mapname/Features/geofence?x={x}&y={y}&role={role}"
                .replace("{x}", pos.coords.longitude)
                .replace("{y}", pos.coords.latitude)
                .replace("{role}", isc.touchgeo.authenticationMgr.getAuthorizationRecord().Role);
            $.getJSON(endpoint, function success(data) {
                $('#debug')
                    .prepend(
                        $('<div></div>').text('features: ' + data.length)
                    )
                    .css({
                        textAlign: 'right',
                        color: 'black'
                    });
                for (layer in data) {
                    if (layer in geofencingRules) {
                        geofencingRules[layer](data[layer]);
                    }
                }
            });
        },
        function error(error) {
            $('#debug')
                .prepend(
                    $('<div></div>').text('error: ' + error.code)
                )
                .css({
                    textAlign: 'right',
                    color: 'black'
                });
        },
        {
            enableHighAccuracy: true,
            maximumAge: maximumAge,
        }
    );
    

    也就是说,将一个初始化为 0 但在第一次回调时递增到 15000 的变量传递给 maximumAge。

    希望这对某人有所帮助。

    【讨论】:

    • 在你的回调中设置 maximumAge 的值实际上除了改变你的局部变量之外还有什么作用吗?设置不是作为值传递而不是作为引用传递吗?
    • 不确定你的意思,但在底部我使用变量作为 watchPosition 函数的第三个选项参数。不过,那 4 年的时间旅行。
    猜你喜欢
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    • 2012-11-14
    • 2012-02-06
    • 2011-09-23
    相关资源
    最近更新 更多