1

I am writing a watchPosition function for a geofencing feature in MapDotNet's Touchgeo (http://www.mapdotnet.com/index.php/component/content/article?id=131). On initial load, everything works excellently; on refresh, I only get one line of debug messages, indicating only one callback, and the GPS on my phone never turns on. Here is my watchPosition function:

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,
    }
);

Any ideas?

4

1 回答 1

2

我想到了。基本上,maximumAgeon positionOptions 告诉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。

希望这可以帮助某人。

于 2013-06-07T19:09:39.227 回答