私はかなり長い間 iOS で地理位置情報を調べようとしましたが、いくつかの奇妙な結論に達しました。
アプリが開いているとき(スタンバイ状態などではない)、地理位置情報は非常に正確です。携帯電話をスタンバイ状態にするか、アプリを最小化すると、AGPS にジャンプして、場所を近くの G タワーなどに変更します。
でも; アプリにマップビューがあり、イベントがトリガーされるたびにユーザーの場所を更新すると、スタンバイでも非スタンバイでも機能するようです。AGPS ではなく通常の GPS にとどまるため、このマップビューのトリガーは何ですか?
マップビューの作成は次のとおりです。
var mapview = Ti.Map.createView({
bottom: -300,
height: 200,
mapType: Ti.Map.STANDARD_TYPE,
region: {
latitude: 0,
longitude: 0,
latitudeDelta: delta,
longitudeDelta: delta
},
animate:true,
regionFit:true,
userLocation:true
});
mainWindow.add(mapview);
そして場所の取り扱い:
//Set a timestamp on the current time. This is being checked later to see if its 5 minutes later(Because setInterval isn't trustworthy check will be done this way)
var realTime = new Date();
realTime = realTime.getTime();
//Set the battery time to be on the currenttime plus 5 minutes
batteryTimeToBe = realTime+batteryTimeIncrement;
//Empty interval and text to make a clean (re)start
clearInterval(interval);
//Set a half second timer on the stop button appearing(So people can't double tap the buttons)
stopTimeout = setTimeout(showStopButton, 1000);
//Switch the textlabels and buttons from startview to stopview
stopText.show();
startText.hide();
btnStart.hide();
//Locationhandler
location.start({
action: function (e) {
if (e.coords) {
mapview.setLocation({
latitude: e.coords.latitude,
longitude: e.coords.longitude,
animate: false,
latitudeDelta: delta,
longitudeDelta: delta
});
//If the newly acquired location is not the same as the last acquired it is allowed
if (e.coords.longitude != lastLon && e.coords.latitude != lastLat) {
//set the last acquired locations+other info to their variables so they can be checked(and used)
lastLat = e.coords.latitude;
lastLon = e.coords.longitude;
lastKnownAltitude = e.coords.altitude;
lastKnownHeading = e.coords.heading;
lastKnownSpeed = e.coords.speed;
if (lastLat != 0 && lastLon != 0) {
setGPSholder(lastLat, lastLon, lastKnownAltitude, lastKnownHeading, lastKnownSpeed);
} else {
GPSSaved.text = 'Geen coordinaten.';
}
}
}
var timeNow = new Date();
timeNow = timeNow.getTime();
//If the now-time is higher or equal to the batteryTimeToBe(Which is reset after every call or when the start button is fired) send the batteryLevel
if (timeNow >= batteryTimeToBe) {
sendBatteryLevel();
batteryTimeToBe = timeNow+batteryTimeIncrement;
timeNow = null;
//Ti.API.info(new Date());
}
}
});
/*
A second interval which shows a counter to the user and makes sure a location is sent
roughly every 5 seconds(setInterval isn't accurate though)
A lot of counters are tracked for several reasons:
minuteInterval: Counter which makes sure the last location is sent after a minute if no new one is found in the meantime
secondsLastSent: The visual counter showing the user how long its been for the last save(Is reset to 0 after a succesful save)
*/
interval = setInterval(function () {
minuteInterval++;
secondsLastSent++;
counterBlock.text = "De laatste locatie is " + secondsLastSent + " seconden geleden verstuurd";
//If the counter is higher than 5 send a new coordinate. If at the same time the minuteInterval is over a minute
//The last location is put in the array before calling the sendCoordinates
if (counter >= 5) {
if (minuteInterval > 60) {
if (lastLat != 0 && lastLon != 0) {
setGPSholder(lastLat, lastLon, lastKnownAltitude, lastKnownHeading, lastKnownSpeed);
}
}
counter = 1;
sendCoordinates();
} else {
counter++;
}
}, 1000);