2014-04-29 157 views
0

我有一個科爾多瓦/ PhoneGap的應用是推動地理位置(經度/緯度)到localStorage的。我有一個問題得到這些變量推入mySQL數據庫。我只是試圖在連接到mySQL時發送數據。我的代碼如下所示:從localStorage的PhoneGap的/科爾多瓦推GeoLocation中瓦爾到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的。現在我需要做的就是在NetworkAvailable上,將數據推送到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(); 

就像說,我的經度和緯度店到數據庫沒有問題。但把它發送到我的MySQL數據庫是我有麻煩的事情。我正在使用這種方法,因爲我正在開發手機(準確地說Cordova/Phonegap)。

任何幫助,這將是一個加號。

回答

0

如果任何人有問題發送localStorage coords ...我能夠得到我的數據發送...我設置我的ajax張貼從localStorage引用科爾多瓦/ 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'); 
     } 
    });