2014-02-27 50 views
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> 
+0

您能否再詳述一下?你的代碼的哪部分工作不正常? 「動態」是什麼意思?你的錯誤處理程序會拋出什麼? – Roberto

+0

每當緯度值發生變化時,我想將showLocation的緯度和經度值傳遞給populateDB方法。 – Vinod

回答

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); 
} 

,如果我理解正確的話您的問題它應該工作。

0

使用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> 
相關問題