JQuery 和地理位置不发送任何数据
Posted
技术标签:
【中文标题】JQuery 和地理位置不发送任何数据【英文标题】:JQuery and geolocation not sending any data 【发布时间】:2015-03-05 13:06:07 【问题描述】:早安,
我正在尝试创建一个脚本来加载我的浏览器地理位置,然后将其发送到保存它的文件。
问题是。数据未发送。 还有一个更大的问题是,我尝试了很多东西,却一无所知。
我添加了几个警报,但没有显示警报。
脚本应该做什么? 每五秒运行一次并请求您的地理位置。 当您在手机上单击接受并接受来自该来源的所有人时,您将获得一个有效的 GPS 类似跟踪。
代码:
<script type="text/javascript">
function success(position)
///SaveActiveGeoLocation();
function error(msg)
var s = document.querySelector('#status');
s.innerhtml = typeof msg == 'string' ? msg : "failed";
s.className = 'fail';
// console.log(arguments);
if(navigator.geolocation)
navigator.geolocation.getCurrentPosition(success, error);
else
error('not supported');
function SaveGeoLocation()
var Lat = position.coords.latitude;
var Lon = position.coords.longitude;
var Accuracy = position.coords.accuracy;
///######## SENDING THE INFORMATION BY AJAX
$.ajax(
type : "POST", /// **** SEND TYPE
url : "savegeo.php", /// **** TARGET FILE TO FETCH THE DATA
data :
'Lat' : Lat,
'Lon' : Lon,
'GeoAccuracy' : Accuracy
,
///######## IN CASE OF SUCCESS
success:function(response)
if( response == "ok" )
alert('SEND!');
else
alert( "Response = " + response );
);
$(document).ready(function()
$.ajaxSetup(
cache: false
); // This part addresses an IE bug. without it, IE will only load the first number and will never refresh
setInterval(function()
///alert('HOI!');
SaveGeoLocation();
, 5000);
// the "10000" here refers to the time to refresh the div. it is in milliseconds.
/// **** DEFAULT LOADING
///SaveGeoLocation();
);
</script>
保存发送 POST 数据的文件:
<?php
include('function.geolocation.class.php');
$geo = new GeoLocation();
$Lat = $_POST['Lat'];
$Lon = $_POST['Lon'];
$GeoAccuracy = $_POST['GeoAccuracy'];
$IP = $geo->GetIP();
$file = 'location.txt';
$address = $geo->getAddress($Lat, $Lon);
$contents = $Lat.'|'.$Lon.'|'.$IP.'|'.$GeoAccuracy.'|'.date('Y-m-d H:i:s').'|'.$address.PHP_EOL;
$handle = fopen($file, 'a');
fwrite($handle, $contents);
fclose($handle);
echo 'ok';
?>
【问题讨论】:
我假设示例代码是在您的实验之后,因为它什么也没做:函数SaveActiveGeoLocation()
与实际 SaveGeoLocation()
更改并在 success()
中注释函数调用。
你为什么要隔段时间打电话给SaveGeoLocation
【参考方案1】:
我可以看到的一个问题是变量position
在SaveGeoLocation
方法的上下文中不存在
function success(position)
//SaveActiveGeoLocation();
window.position = position;
function SaveGeoLocation()
if (!window.position)
return;
//your stuff
不需要使用间隔调用SaveGeoLocation
,你可以像在成功回调中调用SaveGeoLocation
一样
function success(position)
SaveActiveGeoLocation(position);
function SaveGeoLocation(position)
//your stuff
如果你想连续保存位置
$(document).ready(function ()
$.ajaxSetup(
cache: false
);
function saveLocation()
navigator.geolocation.getCurrentPosition(success, error);
function success(position)
var Lat = position.coords.latitude;
var Lon = position.coords.longitude;
var Accuracy = position.coords.accuracy;
///######## SENDING THE INFORMATION BY AJAX
$.ajax(
type: "POST", /// **** SEND TYPE
url: "savegeo.php", /// **** TARGET FILE TO FETCH THE DATA
data:
'Lat': Lat,
'Lon': Lon,
'GeoAccuracy': Accuracy
,
///######## IN CASE OF SUCCESS
success: function (response)
).done(function (response)
if (response == "ok")
alert('SEND!');
else
alert("Response = " + response);
).always(function ()
setTimeout(saveLocation, 5000)
);
function error(msg)
var s = document.querySelector('#status');
s.innerHTML = typeof msg == 'string' ? msg : "failed";
s.className = 'fail';
if (navigator.geolocation)
saveLocation();
else
error('not supported');
);
【讨论】:
但我希望系统继续跟踪用户,以便我们获得实际的当前位置更新。以上是关于JQuery 和地理位置不发送任何数据的主要内容,如果未能解决你的问题,请参考以下文章