watchPosition()只触发一次回调

时间:2013-06-07 17:38:24

标签: android html5 google-chrome geolocation gps

我正在为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,
    }
);

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

我明白了。基本上,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。

希望这有助于某人。