每分钟将用户的地理位置发送到服务器 [英] Send user's geolocation to server every minute

查看:109
本文介绍了每分钟将用户的地理位置发送到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个应用程序,每分钟左右将用户的地理位置发送到MYSQL数据库。这是我现在的代码,但它不起作用。

I'm trying to write a application that sends user's geolocation to MYSQL database every minute or so. This is the code I have right now, but it's not working. How do i need to change this to make it work?

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:

and 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的控制台显示我在属性列表
之后发现

And I suppose this is not the right way to send multiple variables, is it?. Firebug's console shows me

 missing } after property list
[Break On This Error]   

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

当我仅使用一个值时,它将该变量POST给服务器一次,然后在该控制台给出:

When I use only one value, it POSTs that variable to server only once and after that console gives:

position is undefined
[Break On This Error]   

var lat = position.coords.latitude;


推荐答案

您需要执行以下操作:

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

这篇关于每分钟将用户的地理位置发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆