我正在尝试根据我的 mysql 表生成 javascript 函数并运行该生成的函数。当新的多边形坐标添加到数据库时,我想更新我的谷歌地图。
文件上传和转换完成时启动加载功能:
function reloadMaps() {
console.log("reloadMaps");
//$.get("pages/fields.php", function(data){$('#content').html(data);});
$.get("addMaps.php", function(data){$('#mapCoordsInclude').html(data);});
addMaps();
}
addMaps.php 文件:
<script type="text/javascript">
function addMaps(){
clearMap();
console.log("paleistas update");
<?php
include "dbConfig.php";
// $r = mysql_query("SELECT `fields`.id as id, `fields`.coordinates as coordinates, `seedings`.culture_id as culture_id, `cultures`.color as color FROM `fields` INNER JOIN seedings ON `seedings`.field_id = `fields`.id INNER JOIN cultures ON `seedings`.culture_id = `cultures`.id where `fields`.farm_id = '4'" );
$r = mysql_query("SELECT id, coordinates from fields where coordinates !='' and farm_id = 4");
$fields = array();
while($field = mysql_fetch_assoc($r)){
$fields[$field['id']] = $field;
$cord = explode(';', $field['coordinates']);
unset($cord[count($cord)-1]);
echo "console.log(\"Naujausias laukas su koordinatemis ".$field['id']."\");\n";
echo "var laukas".$field['id'].";\n";
echo 'var polygonCoords = [';
foreach ($cord as $key => $c) {
if($key == count($cord)-1)
echo "new google.maps.LatLng(".$c.")";
else
echo "new google.maps.LatLng(".$c."),";
}
echo "];\n";
echo "laukas".$field['id']." = new google.maps.Polygon({
paths: polygonCoords, strokeColor: '#000000', strokeOpacity: 0.8, strokeWeight: 2, fillColor: '#000000', fillOpacity: 0.35, id: ".$field['id']." }); laukai.push(laukas".$field['id'].");\n";
}
?>
<?php
echo"\n";
foreach($fields as $key => $field)
{
echo " laukas". $field['id'].".setMap(map); google.maps.event.addListener(laukas".$field['id'].", 'click', showFieldInfo);\n";
}
?>
}
function showFieldInfo(event) {
if (document.getElementById('fields') != null) {
//alert(this.id);
var page = "pages/showField.php?id=";
page += this.id;
$.get(page, function(data){
$('#content').html(data);
});
}
}
</script>
当我触发此脚本时,它会自动运行,将坐标添加到 BD(一个单独的函数),所有 addMaps.php 都会运行,但 SELECT 语句看不到新插入的数据。如果我通过再次单击上传表单再次运行它,它运行良好。
如果我在第一次上传后从 db 中删除插入的坐标,然后再次运行上传 - 一切正常。
有任何想法吗?