我想每隔一段时间从HTML / PHP网页将GPS坐标发送到服务器。 3秒我将如何发送此信息?
答案 0 :(得分:1)
这将每3秒收集一次浏览器的坐标,并将其发送到名为lat和lon的GET参数的文件processor.php。将其放在HTML页面上的结束body标签之前:
<script>
function sendRequest(position) {
var req = new XMLHttpRequest();
req.onreadystatechange = function() {
if(this.readyState == 4 && this.status == 200) {
setTimeout(function() {
navigator.geolocation.getCurrentPosition(sendRequest);
}, 3000);
}
};
req.open('GET', 'processor.php?lat='+position.coords.latitude+'&lon='+position.coords.longitude, true);
req.send();
}
navigator.geolocation.getCurrentPosition(sendRequest);
</script>
将此PHP代码放置在processor.php中:
<?php
$lat = $_GET['lat'];
$lon = $_GET['lon'];
//update SQL database as necessary with above variables
?>