我正在使用watchPosition方法连续获取坐标。我在showLocation方法中得到了我的纬度和经度。现在我想将我获得的所有纬度和经度存储到数据库中,所以我在showLocation方法中编写了db.transcation。现在我如何从showLocation方法获得动态纬度和经度到populateDB方法。我想在每次更改时存储纬度和经度。
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready", onDeviceReady, false);
var db = window.openDatabase("GPS", "1.0", "Simple GPS", 500000); //will create database GPS or open it
function onDeviceReady()
{
var watchID = null;
if(navigator.geolocation)
{
var options = { timeout: 5000, enableHighAccuracy: true };
watchID = navigator.geolocation.watchPosition(showLocation, errorHandler, options);
}
}
function populateDB(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS location (id INTEGER PRIMARY KEY AUTOINCREMENT, latitude TEXT, longitude TEXT, LastModifiedTime CHAR(30))');
tx.executeSql('INSERT INTO location(latitude,longitude,LastModifiedTime) VALUES ("-----", "------", datetime('now'))');
}
function showLocation(position)
{
// Getting Lat/Longs from call back method
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
// Display coordinates in html / screen..
document.getElementById("demo").innerHTML="<p>Latitude : " + latitude + "</p><p> Longitude: " + longitude +"</p>";
document.getElementById("messageTxt").innerHTML="Latitude : " + latitude + " Longitude: " + longitude;
document.getElementById("MailLatlongs").innerHTML="Latitude : " + latitude + " Longitude: " + longitude;
db.transaction(populateDB, errorCB, successCB);
}
</script>
答案 0 :(得分:2)
我认为你想在事务回调函数中传递参数。 如果这是您的问题,请参阅此链接。
答案 1 :(得分:0)
尝试将它们分配为全局变量:
var latitude;
var longitude;
function populateDB(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS location (id INTEGER PRIMARY KEY AUTOINCREMENT, latitude TEXT, longitude TEXT, LastModifiedTime CHAR(30))');
tx.executeSql('INSERT INTO location(latitude,longitude,LastModifiedTime) VALUES ("-----", "------", datetime('now'))');
}
function showLocation(position) {
// Getting Lat/Longs from call back method
latitude = position.coords.latitude;
longitude = position.coords.longitude;
// Display coordinates in html / screen..
document.getElementById("demo").innerHTML="<p>Latitude : " + latitude + "</p><p> Longitude: " + longitude +"</p>";
document.getElementById("messageTxt").innerHTML="Latitude : " + latitude + " Longitude: " + longitude;
document.getElementById("MailLatlongs").innerHTML="Latitude : " + latitude + " Longitude: " + longitude;
db.transaction(populateDB, errorCB, successCB);
}
如果我理解你的问题,它应该有效。
答案 2 :(得分:0)
使用setTimeout。对于每个间隔。检查位置是否已更改。如果更改,请将其发送到db。 http://www.w3schools.com/jsref/met_win_settimeout.asp
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready", onDeviceReady, false);
var db = window.openDatabase("GPS", "1.0", "Simple GPS", 500000); //will create database GPS or open it
function onDeviceReady()
{
var watchID = null;
if(navigator.geolocation)
{
var options = { timeout: 5000, enableHighAccuracy: true };
watchID = navigator.geolocation.watchPosition(showLocation, errorHandler, options);
}
initChecking();
}
function initChecking() {
setTimout(checkLocation, 1000);
}
var prevLatitude, prevLngitude;
function checkLocation() {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
if(prevLatitude == latitude || prevLngitude == longitude) {
//Code to populateDB
prevLatitude = latitude;
prevLngitude == longitude;
}
initChecking();
}
function populateDB(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS location (id INTEGER PRIMARY KEY AUTOINCREMENT, latitude TEXT, longitude TEXT, LastModifiedTime CHAR(30))');
tx.executeSql('INSERT INTO location(latitude,longitude,LastModifiedTime) VALUES ("-----", "------", datetime('now'))');
}
function showLocation(position)
{
// Getting Lat/Longs from call back method
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
// Display coordinates in html / screen..
document.getElementById("demo").innerHTML="<p>Latitude : " + latitude + "</p><p> Longitude: " + longitude +"</p>";
document.getElementById("messageTxt").innerHTML="Latitude : " + latitude + " Longitude: " + longitude;
document.getElementById("MailLatlongs").innerHTML="Latitude : " + latitude + " Longitude: " + longitude;
db.transaction(populateDB, errorCB, successCB);
}
</script>