我正在使用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>
您能否再詳述一下?你的代碼的哪部分工作不正常? 「動態」是什麼意思?你的錯誤處理程序會拋出什麼? – Roberto
每當緯度值發生變化時,我想將showLocation的緯度和經度值傳遞給populateDB方法。 – Vinod