我正在使用 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>