【问题标题】:Send user's geolocation to server every minute每分钟将用户的地理位置发送到服务器
【发布时间】:2012-05-23 15:28:13
【问题描述】:

我正在尝试编写一个应用程序,每分钟左右将用户的地理位置发送到 MYSQL 数据库。这是我现在拥有的代码,但它不起作用。我需要如何更改它才能使其正常工作?

JS:

<!DOCTYPE html>
<html>
<head>
<script src="js/jquery.js"></script>
</head>

<body>
<script>
setInterval ( "onPositionUpdate()", 10000 );
function onPositionUpdate(position)
            {
                var lat = position.coords.latitude;
                var lng = position.coords.longitude;
                jQuery.ajax({ type: "POST", 
                    url:  "myURL/location.php", 
                    data: 'x='+lat+ '&y='+lng , 
                    cache: false, 
                        });
                        }

            if(navigator.geolocation)
               navigator.geolocation.watchPosition(onPositionUpdate);
            else
                alert("navigator.geolocation is not available");



</script>
</body>
</html>

和 PHP:

<?php
  include 'config.php';

  // database connection
  $conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);

  // new data

  $x = @$_POST['x'];
  $y = @$_POST['y'];
  // query
  $sql = "update locations set x=?, y=? where username = asd";
  $q = $conn->prepare($sql);
  $q->execute(array($x), ($y));
?>

我想这不是发送多个变量的正确方法,是吗? Firebug 的控制台显示了我

 missing } after property list
[Break On This Error]   

data: 'x='+lon+'&y='+lat;

当我只使用一个值时,它只会将该变量发布到服务器一次,然后控制台给出:

position is undefined
[Break On This Error]   

var lat = position.coords.latitude;

【问题讨论】:

    标签: php jquery mysql geolocation


    【解决方案1】:

    您需要执行以下操作:

    var currPosition;
    navigator.geolocation.getCurrentPosition(function(position) {
        updatePosition(position);
        setInterval(function(){
            var lat = currPosition.coords.latitude;
            var lng = currPosition.coords.longitude;
            jQuery.ajax({
                type: "POST", 
                url:  "myURL/location.php", 
                data: 'x='+lat+'&y='+lng, 
                cache: false
            });
        }, 1000);
    }, errorCallback); 
    
    var watchID = navigator.geolocation.watchPosition(function(position) {
        updatePosition(position);
    });
    
    function updatePosition( position ){
        currPosition = position;
    }
    
    function errorCallback(error) {
        var msg = "Can't get your location. Error = ";
        if (error.code == 1)
            msg += "PERMISSION_DENIED";
        else if (error.code == 2)
            msg += "POSITION_UNAVAILABLE";
        else if (error.code == 3)
            msg += "TIMEOUT";
        msg += ", msg = "+error.message;
    
        alert(msg);
    }
    

    【讨论】:

    • 如何每分钟发送一次位置? setInterval("updatePosition()", 60000);?
    • 为什么需要每分钟发送一次位置?它会自动发送,它会改变
    • 哦,好的。控制台显示它只发布一次,所以我认为它只会发送一次位置。但是谢谢,这会工作
    • 对不起,我想我不能接受这个答案。因为如果每次位置变化时都将值发送到服务器,如果有很多设备同时发送值,服务器将非常困难。所以我认为我需要 watchPosition,但只每分钟发送一次值。你能给我提供一个示例代码吗?
    • 每分钟发送位置将是最糟糕的,然后仅在服务器发生更改时发送位置,您可以在PHP 脚本中存储当前位置的session,而不是发送AJAX每分钟请求一次:/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多