我有一个Cordova/Phonegap应用程序,可以将地理位置(经度/纬度)推送到本地存储。我有一个问题,让这些变量推到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();
在将数据发送到本地存储时工作正常。现在我需要做的就是onNetworkAvailable,将数据推送到mySQL。
My 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;
}
以及要发送到mySQL的dbUserLocation.php:
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)。
这方面的任何帮助都是有利的。
最佳答案
如果有人有问题发送本地存储坐标…我可以让我的数据发送。。。我设置了ajax来发布引用本地存储中Cordova/Phonegaps getItem的新vars。我的代码如下。。。工作完美!希望这能帮助任何人实现同样的目标。
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');
}
});
关于php - PhoneGap/Cordova将GeoLocation从localStorage vars推送到MySQL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23353428/