每15秒将用户地理位置保存到MySQL中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了每15秒将用户地理位置保存到MySQL中相关的知识,希望对你有一定的参考价值。
我需要能够在标记移动时以15秒的间隔连续存储用户的地理位置,尽管其纬度和经度是连续的。
目前,该应用仅在加载页面时才存储用户的地理位置
我想知道如何将我的代码重构为数据来持久化。
heatmap.php
<script>
// specify the default lat long
var map,
currentPositionMarker,
map;
// change the zoom if you want
function initializeMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 16,
});
heatmap = new google.maps.visualization.HeatmapLayer({
data: getPoints(),
map: map
});
}
function locError(error) {
// tell the user if the current position could not be located
alert("The current position could not be found!");
}
// current position of the user
function setCurrentPosition(pos) {
currentPositionMarker = new google.maps.Marker({
label: {
color: 'black',
fontWeight: 'bold',
text: 'CD',
},
map: map,
position: new google.maps.LatLng(
pos.coords.latitude,
pos.coords.longitude,
),
title: "Current Position"
});
// sends var a AND var b from users gps over to personlocator.php to get stored in db
var a = pos.coords.latitude;
var b = pos.coords.longitude;
console.log(a,b);
$.ajax({
type: "POST",
url: "personlocator.php",
data: {
a : a,
b : b
}
});
map.panTo(new google.maps.LatLng(
pos.coords.latitude,
pos.coords.longitude
));
} // end of function
function displayAndWatch(position) {
// set current position
setCurrentPosition(position);
// watch position
watchCurrentPosition();
}
function watchCurrentPosition() {
var positionTimer = navigator.geolocation.watchPosition(
function (position) {
setMarkerPosition(
currentPositionMarker,
position
);
});
}
function setMarkerPosition(marker, position) {
marker.setPosition(
new google.maps.LatLng(
position.coords.latitude,
position.coords.longitude)
);
}
function initLocationProcedure() {
initializeMap();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(displayAndWatch, locError);
} else {
// tell the user if a browser doesn't support this amazing API
alert("Your browser does not support the Geolocation API!");
}
}
</script>
personlocator.php
if (isset($_POST['a'], $_POST['b'])) {
$latitude = escape_string($_POST['a']); // stores var 'a' which is passed over here from initMap()
$longitude = escape_string($_POST['b']); // stores var 'b' which is passed over here from initMap()
$_SESSION['latitude'] = $latitude;
$_SESSION['longitude'] = $longitude;
$insert_query = query("INSERT INTO users(user_latitude, user_longitude) VALUES('{$_SESSION['latitude']}','{$_SESSION['longitude']}') ");
confirm($query);
} // ends if isset
答案
您可以使用javascript中的setInterval
完成此操作
setInterval(function(){
// some api to send geolocations
}, 15000);
//15 seconds
以上是关于每15秒将用户地理位置保存到MySQL中的主要内容,如果未能解决你的问题,请参考以下文章
如何通过单击适配器类中代码的项目中的删除按钮来删除列表视图中的项目后重新加载片段?
如何在 Amazon DynamoDB 中保存实时和历史车辆位置数据