【发布时间】:2020-03-07 01:15:25
【问题描述】:
我目前有一个 html 页面,分为 2 列 - 右侧是带有标记的传单地图,左侧描述了有关标记的更多信息。我使用右侧的 geoJson 数据加载 Leaflet 地图,地图和标记显示正常。现在我想取相同的geoJson数据并用它来填充php页面的左侧。这样,在我们添加站点时,只需要更新一个文件。
这是我的 geoJson 数据示例:
var historicalMarkers = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-77.423603, 38.865608 ]
},
"properties": {
"id": 1,
"siteName": "House14"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [ -77.416770, 38.923650]
},
"properties": {
"id": 2,
"siteName": "Meeting House"
}
}..........
在主 php 页面上,我只包含我的 geoJson 文件 (places.js) 和我的 geoJson/Leaflet 代码 (map-geoJson.js)。
在这个主要的 php 页面上,我想要每个标记的描述。在 map-geoJson.js 中,我通过 ajax 将数据传递回 php,如下所示:
// get stuff ready for php
var historicalData = [];
for (var i = 0; i < historicalMarkers.features.length; i++) {
var currentFeature = historicalMarkers.features[i];
var id = currentFeature.properties.id;
var siteName = currentFeature.properties.siteName;
console.log(id, siteName);
historicalData.push({ id: id, siteName : siteName });
}
var historicalJson = JSON.stringify(historicalData);
$.ajax({
url: location.pathname, //current page
type: 'POST',
data: historicalJson
});
控制台上显示的数据是正确的。在我的 php 页面上,我想抓取数据并将其放入页面上的几个不同的 div 中:
$phpArray = json_decode($_POST['historicalJson']);
var_dump ($phpArray); // NULL is displayed
foreach ( $phpArray as $value )
{ ?>
<div class="id">
<h4><a href="#"><?php echo $value['id']; ?></a></h4>
</div>
<div class="siteName">
<h4><a href="#"><?php echo $value['siteName']; ?></a></h4>
</div>
......
<?php } ?>
但是什么都没有显示。我也试过回显
echo $value[0]->id;
echo $value['id']
不知何故,我没有在 php 页面中正确抓取数据并将其回显到 div 中。 任何人都可以提出建议吗?我确定这是我缺少的一些小东西,但是我尝试了很多很多方法来获取 php.ini 中的数据。 谢谢
【问题讨论】:
-
historicalMarkers在运行时的值是多少? -
historicalMarkers 包含我的 geoJson 数据。 (这是你的意思吗?)
-
不,我的意思是你在运行时调试得到的实际值。
console.log(historicalMarkers)的输出是什么? -
这是一个包含所有信息的对象。如果有帮助,我将代码上传到测试站点:link
标签: javascript php ajax leaflet geojson