如何动态地将值从一种方法传递给另一种方法?
问题描述:
我正在使用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>
答
尽量只将它们分配为全局:
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);
}
,如果我理解正确的话您的问题它应该工作。
答
使用setTimeout。每隔一段时间。检查位置是否改变。如果更改发送到数据库。 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>
您能否再详述一下?你的代码的哪部分工作不正常? “动态”是什么意思?你的错误处理程序会抛出什么? – Roberto
每当纬度值发生变化时,我想将showLocation的纬度和经度值传递给populateDB方法。 – Vinod