【问题标题】:How to pass values from one method to another in javascript dynamically?如何在javascript中动态地将值从一种方法传递到另一种方法?
【发布时间】:2014-02-27 07:15:15
【问题描述】:

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

【问题讨论】:

  • 您能详细说明一下吗?您的代码的哪一部分工作不正常?你所说的“动态”是什么意思?你的错误处理程序会抛出任何东西吗?
  • 每当纬度值发生变化时,我想将纬度和经度值从 showLocation 传递给 populateDB 方法。

标签: javascript jquery web-sql w3c-geolocation


【解决方案1】:

我想你想在事务回调函数中传递参数。 如果这是您的问题,请参考此链接。

how can I pass argument in transaction callback function

【讨论】:

    【解决方案2】:

    尝试将它们分配为全局变量:

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

    如果我正确理解了您的问题,它应该可以工作。

    【讨论】:

      【解决方案3】:

      使用 setTimeout。对于每个间隔。检查位置是否已更改。如果更改将其发送到 db。 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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-29
        • 1970-01-01
        • 2018-12-24
        相关资源
        最近更新 更多