【发布时间】:2017-03-23 11:54:40
【问题描述】:
我有一个填充了纬度和经度的 MySQL 数据库。
数据在 Google 地图上绘制成折线。 一切正常,但我不知道如何在添加新数据时自动更新折线。
我尝试在函数 initmap 上设置间隔,但没有奏效。 如果有人可以帮助我,我会很高兴。
<!DOCTYPE html >
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<style>
#map {
height: 100%;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: new google.maps.LatLng(59.913063,10.750923),
zoom: 10,
mapTypeId : 'terrain'
});
var flightPlanCoordinates = [
<?php
require("dbcred.php");
$connection = mysqli_connect($host, $username, $password, $database);
mysqli_select_db($connection, $database);
$query = mysqli_query($connection,"SELECT latitude, longitude FROM geografi");
while($row = mysqli_fetch_assoc($query)){
$lat = $row['latitude'];
$lon = $row['longitude'];
echo 'new google.maps.LatLng('.$lat.', '.$lon.'),';
}
?>
];
var flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
flightPath.setMap(map);
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=GoogleKey&callback=initMap">
</script>
</body>
</html>
【问题讨论】:
-
您需要使用带间隔的 Ajax 请求。如果能把js代码和php代码分开就更好了
标签: javascript php mysql google-maps