1

我正在为 Cordova 2.0.0 开发。我正在 android 2.2 上测试我的应用程序。现在我得到了获取用户位置的代码,然后用标记在谷歌地图中显示它。首先我检索地理位置代码并将其显示在谷歌地图中:

> function onLoad() { document.addEventListener("deviceready",
> onDeviceReady, false); }
> 
> function onDeviceReady() {
> 
> }
> 
> var onSuccess = function(position) {
>     var myLat = position.coords.latitude;
>     var myLong = position.coords.longitude;
> 
>     
>     var mapOptions = {
>         center: new google.maps.LatLng(myLat, myLong),
>         zoom: 14,
>         mapTypeId: google.maps.MapTypeId.ROADMAP
>     };
> 
> var map = new google.maps.Map(document.getElementById("map_canvas"),
>                                   mapOptions);
> 
>     var marker = new google.maps.Marker({
>                                    position: new google.maps.LatLng(myLat, myLong),
>                                         map: map,
>                                         title:"Hello World!"
>                                         });
> 
> };
> 
> 
> function onError(error) {
>     alert('code: '    + error.code    + '\n' +
>           'message: ' + error.message + '\n'); }
> 
> 
> 
> navigator.geolocation.getCurrentPosition(onSuccess, onError,
>       {'enableHighAccuracy':true,'timeout':10000});

现在是我的数据库的代码,我使用了来自http://docs.phonegap.com/en/2.0.0/cordova_storage_storage.md.html#Storage的示例:

> <!DOCTYPE html> <html>   <head>
>     <title>Storage Example</title>
>     <script type="text/javascript" src="jqtouch/jquery.js" charset="utf-8"></script>
>     <script type="text/javascript" src="jqtouch/jqtouch.js" charset="utf-8"></script>
>     <script type="text/javascript" charset="utf-8" src="cordova-2.0.0.js"></script>
>     <script type="text/javascript" charset="utf-8">
> 
>     // Wait for Cordova to load
>     //
>     document.addEventListener("deviceready", onDeviceReady, false);
> 
>     // Populate the database 
>     //
>     function populateDB(tx) {
>         tx.executeSql('DROP TABLE IF EXISTS DEMO');
>         tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, longitude, latitude)');
>         tx.executeSql('INSERT INTO DEMO (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, longitude, latitude);
>     }
> 
>     // Query the database
>     //
>     function queryDB(tx) {
>         tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
>     }
> 
>     // Query the success callback
>     //
>     function querySuccess(tx, results) {   console.log("Returned rows = " + results.rows.length);   if (!results.rowsAffected) {
>     console.log('No rows affected!');
>     return false;   }   // for an insert statement, this property will return the ID of the last inserted row   console.log("Last inserted
> row ID = " + results.insertId);
>     }
> 
>     // Transaction error callback
>       function errorCB(err) {
>        console.log("Error processing SQL: "+err.code);
>     }
> 
>     // Transaction success callback
>    
>     function successCB() {
>         var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
>         db.transaction(queryDB, errorCB);
>     }
> 
>     // Cordova is ready
>     //    function onDeviceReady() {
>         var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
>         db.transaction(populateDB, errorCB, successCB);
>     }
> 
>     </script>   </head>   <body>
>     <h1>Example</h1>
>     <p>Database</p>   </body> </html>

我不确定如何将从我的 GPS(地理位置)中检索到的长/纬度值放入我的数据库中。非常感谢任何教程、示例代码或建议。

我一直在想这样的事情:

> //GEOLOCATION 
>     var onSuccess = function(position) {
>     var myLat = position.coords.latitude;
>     var myLong = position.coords.longitude;
>     insertEntry(latitude, longitude);
>     //MAP
> 
> 
> function insertEntry(latitude, longitude) {
>     db.transaction(
>         function(populateDB) {
>             tx.executeSql(
>                 'INSERT INTO entries (latitude, longitude) ' + 
>                     'VALUES (?, ?);',
>                 [latitude, longitude],
>                 function(){
>                    }, 
>                 errorHandler
>             );
>         }
>     );
4

1 回答 1

1

您需要打开一个数据库:

var db = window.openDatabase("dbName", "1.0", "Db Name", 100000);

然后

db.transaction(
     function(populateDB) 
        .....

检查Phonegap 存储文档

于 2012-09-18T21:31:49.390 回答