我有一個包含JS和PHP代碼的index.php。 PHP代碼從iOS應用程序讀取GPS座標,JS將這些座標讀回到Web應用程序中顯示。從javascript調用php
我被困在搞清楚如何每隔幾秒調用一次PHP,以便我可以獲得新的座標。我對如何解決這個問題毫無頭緒,如果我需要爲此目的調用同一個文件,這看起來像是一個遞歸。
刷新php代碼並讀取新座標的最佳方法是什麼?這是我的代碼:
<?php include_once('location.php') ?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>My GeoLocation</title>
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
<script>
var map;
google.maps.visualRefresh = true;
function initialize() {
var myLatlng = new google.maps.LatLng(<?php echo $current_lat ?>, <?php echo $current_long ?>);
var mapOptions = {
zoom: 14,
center: myLatlng
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: 'Hello World!'
});
yourFunction();
}
function yourFunction(){
alert ("<?php echo $current_lat ?>, <?php echo $current_long ?>"); --> here I get the same coordinates
//Will add code here to display the new lat/long once I figure out how to refresh them
setTimeout(yourFunction, 1000);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>
$content = file_get_contents('php://input');
$post_data = json_decode($content , true);
$lat = $post_data['lat'];
$long = $post_data['long'];
//CONNECT TO MYSQL
$con1 = mysql_connect("localhost", "aaaaaa", "bbbbbb", "location111");
if ($con1->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$db_selected = mysql_select_db('location111');
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
if (!empty($lat)) {
$sql = "INSERT INTO LocationInfo (latitude, longitude)
VALUES ('$lat', '$long');";
mysql_query($sql) or die ('Error updating database: ' . mysql_error());
}
$read_query = "SELECT * FROM LocationInfo;";
$results = mysql_query($read_query) or die ('Error reading from database: ' . mysql_error());
$column = array();
while($row = mysql_fetch_array($results)){
$column[] = $row;
}
$current_lat = $column[sizeof($column) - 1]['latitude'];
$current_long = $column[sizeof($column) - 1]['longitude'];
?>
不知道你是什麼意思?我有一個iPhone應用程序,它使用POST調用這個腳本並使用JS從它讀取。爲什麼這是一個問題? – moshikafya
...沒關係。我誤解了你的問題 - 我以爲你正在渲染一個網頁,並試圖讓PHP重新運行在頁面上。 –