JQuery和地理位置不发送任何数据

时间:2015-03-05 13:06:08

标签: php jquery geolocation

美好的一天,

我正在尝试创建一个加载浏览器地理位置的脚本,然后将其发送到保存它的文件。

问题是。数据无法发送。 更大的问题是我尝试了很多东西,但我很无能为力。

我添加了几个提醒,但警报没有显示。

脚本应该做什么? 每五秒运行一次并请求您的GeoLocation。 当您点击手机上的“接受”并接受来自此来源的所有内容时,您将获得有效的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';
?>

1 个答案:

答案 0 :(得分:0)

我可以看到的一个问题是变量positionSaveGeoLocation方法的上下文中不存在

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');
    }

});