PhoneGap / Cordova将GeoLocation从localStorage vars推送到MySQL

时间:2014-04-29 00:25:16

标签: php mysql ajax html5 cordova

我有一个Cordova / Phonegap App,可以将地理位置(经度/纬度)推送到localStorage。我在将这些变量推入mySQL数据库时遇到了问题。我只是试图在连接到mySQL时发送数据。我的代码如下:

index.html(地理位置设置)

    //find location info//
    function geolocation(){
        var options = enableHighAccuracy = true;
      watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);

    //this functions runs if geolocation is returned//
        function onSuccess(position) {
            var lat = position.coords.latitude;
            var lng = position.coords.longitude;
            //save to local storage
            localStorage.setItem("latitude", lat);
            localStorage.setItem("longitude", lng);
            //tell user their lng and lat
            //alert("lat: " + lat + "long: " + lng);
        }
        //if cant get location give error//
        function onError(error) {
            alert("message: " + error.message);
            localStorage.setItem("message", error.message);
         }
        }
    geolocation();

在向localStorage发送数据时工作正常。现在,我需要做的就是onNetworkAvailable,将数据推送到mySQL。

我的ondevice.js(在我的脑海中引用)

    // Wait for PhoneGap to load
//
function onDeviceReady() {
    if (parseFloat(window.device.version) === 7.0) {
          document.body.style.marginTop = "20px";
    }
      }

    document.addEventListener("deviceready", onDeviceReady, false);

    // PhoneGap is loaded and it is now safe to make calls PhoneGap methods

var isNetworkAvailable=true;

function onDeviceReady() {
    checkConnection();
    document.addEventListener("offline", setOffline, false);
    document.addEventListener("online", setOnline, false);
     }

     function checkConnection() {
         var networkState = navigator.network.connection.type;

     var states = {};
     states[Connection.UNKNOWN]  = 'Unknown connection';
     states[Connection.ETHERNET] = 'Ethernet connection';
     states[Connection.WIFI]     = 'WiFi connection';
     states[Connection.CELL_2G]  = 'Cell 2G connection';
     states[Connection.CELL_3G]  = 'Cell 3G connection';
     states[Connection.CELL_4G]  = 'Cell 4G connection';
     states[Connection.NONE]     = 'No network connection';

     alert('Connection type: ' + states[networkState]);
     }

 function networkAvailable() {
      return isNetworkAvailable;

     //var userID = $(this).attr('id');
    var getlat = window.localStorage.getItem("latitude", lat);
    var getlng = window.localStorage.getItem("longitude", lng);
     //alert($(this).attr('id'));
            $.ajax({
                type: "POST",
                url: 'myurl/dbUserLocation.php',
                data: { getlat : getlat, getlng : getlng},
                success: function(data)
                {
                    alert("Data sent!");
                },
        complete:function remove(){
        localStorage.removeItem("latitude");
        localStorage.removeItem("longitude");
        },
        error: function(){
        alert('Code 303A');
        }
                });
            }

    function setOffline() {
             isNetworkAvailable=false;
    }

    function setOnline() {
            isNetworkAvailable=true;
    }   

我的dbUserLocation.php发送到mySQL:

      ini_set('display_errors',1); 
      error_reporting(E_ALL);

      header('Access-Control-Allow-Origin: *');  

      $hostdb = 'myhostip';
      $namedb = 'mydbname';
      $userdb = 'root';
      $passdb = 'mydbpass';

      $conn = new PDO("mysql:host=dbip; dbname=$namedb", $userdb, $passdb);
      $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);

      if(isset($_POST['longitude']) && ($_POST['latitude'])) {

      $longitude = $_POST['longitude'];
      $latitude = $_POST['latitude'];

      $stmt = "INSERT INTO row_location (latitude,longitude) 
               VALUES (:longitude,:latitude)";

      $q = $conn->prepare($stmt);
      $results = $q->execute(array(
        ":longitude"=>$longitude,
        ":latitude"=>$latitude
     ));
      }
      exit();

如上所述,我的经度和纬度存储到DB没有问题。但是让它发送到我的mySQL数据库是我无法做到的。我正在使用这种方法,因为我正在开发移动设备(确切地说是Cordova / Phonegap)。

对此有任何帮助都是一个加分。

1 个答案:

答案 0 :(得分:0)

如果有人发送localStorage coords有问题...我能够发送我的数据...我设置了我的ajax来POST我的新vars,它们引用了localStorage的Cordova / Phonegaps getItem。我的代码如下......工作完美!希望这有助于任何人尝试实现同样的目标。

    var getlat = window.localStorage.getItem("latitude");
    var getlng = window.localStorage.getItem("longitude");
    $.ajax({
        type: "POST",
        url: 'http:myurl/dbUserLocation.php',
        data: { latitude : getlat, longitude : getlng },
        success: function(data)
        {
            alert("Location Data Sent!");
        },
        complete:function remove(){
            localStorage.removeItem("latitude");
            localStorage.removeItem("longitude");
        },
        error: function(){
            alert('Code 303A');
        }
    });