0

私は 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>
4

3 に答える 3

2

トランザクションのコールバック関数で引数を渡したいと思います。それが問題である場合は、このリンクを参照してください。

トランザクション コールバック関数で引数を渡すにはどうすればよいですか

于 2014-02-27T07:29:10.470 に答える
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);
}

あなたの問題を正しく理解していれば、うまくいくはずです。

于 2014-02-27T07:23:08.787 に答える