我是新来的,所以要好。 我希望这个gps跟踪器在加载页面“gps.php”时自动将一个singel条目写入我的mysql数据库 如果按下启动按钮,按下停止按钮,我就可以正常工作了。 我想完全摆脱按钮,只需运行一次这个jquery脚本,然后输入一个注入mysql表。
所有代码都在http://cdpn.io/qkEvA
请你帮忙,请帮忙,我一直在为WEEKS(我不是一个程序员作为专业)而奋斗;)
$(document).ready(function() {
// how often should we send location data? in seconds
var sendInterval = 0.9;
var runnerId;
while (!runnerId){
runnerId = prompt("Enter your album name:", "");
}
var intervalId;
var watchId;
var index = 0;
var formData = {};
$("#status p").text("Not tracking");
$('#start').on("click", startTrack);
$('#stop').on("click", stopTrack);
function startTrack(){
if(navigator.geolocation){
watchId = navigator.geolocation.watchPosition(geo_success, errorHandler,
{enableHighAccuracy:true, maximumAge:30000, timeout:27000});
}
else{
alert("Sorry, device does not support geolocation! Update your browser.");
}
}
function stopTrack(){
clearInterval(intervalId);
navigator.geolocation.clearWatch(watchId);
index = 0;
$("#status p").text("Not tracking").removeClass("active").addClass("stopped");
$("#start").removeAttr("disabled");
$("#stop").attr("disabled", "disabled");
}
function geo_success(position){
$("#status p").text("Tracking active").removeClass("stopped").addClass("active");
$("#start").attr("disabled", "disabled");
$("#stop").removeAttr("disabled");
lat = position.coords.latitude;
lon = position.coords.longitude;
formData.lat=lat;
formData.lon=lon;
if(index === 0){
intervalId = setInterval(postData, sendInterval*1000);
}
index++;
}
function addTime(){
// insert time in formData-object
var d = new Date();
var d_utc = ISODateString(d);
formData.time=d_utc;
// date to ISO 8601,
// developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date#Example.3a_ISO_8601_formatted_dates
function ISODateString(d){
function pad(n){return n<10 ? '0'+n : n}
return d.getUTCFullYear()+'-'
+ pad(d.getUTCMonth()+1)+'-'
+ pad(d.getUTCDate())+'D'
+ pad(d.getUTCHours()+2)+':'
+ pad(d.getUTCMinutes())+':'
+ pad(d.getUTCSeconds())+'T'
}
}
function postData(){
addTime();
$.ajax({
type: 'POST',
url: 'save.php?file='+runnerId,
data: formData,
async: false,
});
}
function errorHandler(err){
if(err.code == 1) {
alert("Error: Access was denied");
}
else if(err.code == 2) {
alert("Error: Position is unavailable");
}
}
});
答案 0 :(得分:0)
如果您希望在页面加载后立即开始跟踪并在关闭页面之前停止堆叠,则应在代码中替换:
$('#start').on("click", startTrack);
$('#stop').on("click", stopTrack);
到此:
startTrack();
$( window ).on( 'unload', stopTrack );
并将此代码移至startTrack
函数定义。