【问题标题】:geojson data loaded into leaflet map and php/html pagegeojson 数据加载到传单地图和 php/html 页面中
【发布时间】: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


【解决方案1】:

json_decode() 默认返回一个对象 (stdClass)。如果要将数据用作数组,则需要将函数的第二个参数设置为true

 $phpArray = json_decode($_POST['historicalJson'], true);

json_decode documentation

【讨论】:

  • 该死,错过了。但是,当我这样做时,然后var_dum($phpArray),我仍然得到一个 NULL 响应。我确实在 Firefox 的控制台中看到数据返回 -> XHR->Params: [{"id":1,"siteName":"House"},{"id":2,"siteName":"House2"}]
  • 可能是拼写错误 $_POST['data'] 而不是 $_POST['historicalJson']historicalJson 是您的 javascript 变量,但您将其发送到 data
  • 是的,这是一个错字 - 我有 $_POST['data']。还是纳达。如果有帮助,我将代码上传到测试站点:link
猜你喜欢
  • 2014-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-02
  • 1970-01-01
  • 2015-10-21
  • 1970-01-01
相关资源
最近更新 更多