我目前正在尝试制作一个应用程序,将用户的当前位置上传到我的mysql数据库中。我知道如何在javascript中使用谷歌地图。我有足够的知识使php工作。
我不知道的是,我如何获取我的当前位置,并不断地在数据库中循环。我希望在没有Post&获取变量。如果任何人有任何信息,任何文件或任何东西,都将是伟大的。我试着查了一下。
我发现有人说要使用AJAX,但我不确定如果没有某种交互,它会如何在我当前的位置循环。
我还看到另一个在讨论将其转换为JSON数据。
任何想法都将不胜感激!
我将用简化的概念代码回答,让您了解大致想法。
首先,您需要使用Javascript在web应用程序中添加位置轮询功能:
// We only start location update process if browser supports it
if ("geolocation" in navigator)
{
request_location();
}
// Requesting location from user
function request_location()
{
// Will repeat the process in two minutes
setTimeout(request_location, 1000*60*2);
// Get location info from browser and pass it to updating function
navigator.geolocation.getCurrentPosition(update_location);
}
// Sending location to server via POST request
function update_location(position)
{
// For simplicity of example we'll
// send POST AJAX request with jQuery
$.post("/update-location.php",
{
latitude : position.coords.latitude,
longtitude : position.coords.longitude
},
function(){
// Position sent, may update the map
});
}
然后在服务器端,您必须接收来自上述AJAX请求的坐标:
<?php
$latitude = filter_input(INPUT_POST, 'latitude', FILTER_VALIDATE_FLOAT);
$longtitude = filter_input(INPUT_POST, 'longtitude', FILTER_VALIDATE_FLOAT);
if($latitude && $longtitude)
{
// @TODO: Update database with this data.
}