duanqinbi9029 2019-08-13 12:54
浏览 73

如何使用PHP和Javascript将MySQL中的数据显示到标记上的传单弹出窗口中?

Ok, this is my first time asking a question here. Please don't bully me.

This has been asked before and I tried to solve it myself, yet it just won't work.

Here is my problem: As stated in the question, I can't seem to call the data from MySQL into the marker's popup.

I tried to use <code>json_encode</code> to print it in the Javascript popup.The result is either show [object] [object] in the popup, or the leaflet map won't load at all. If I remove the <code>marker.bindPopup()</code>, the code works fine.

Here is the code :

<div id="map" style="width: 800px; height: 500px;"></div>

              <?php
                  $result = mysqli_query($konek,"SELECT dep,mag FROM data_gempa_jawa");
                  $rows = array();
                  while($r = mysqli_fetch_assoc($result)) {
                      $rows[] = $r;
                  }
              ?>

              <!-- This is for the leaflet maps -->
              <script type="text/javascript">

                <?php include "planelatlong.php"; ?>

                var tiles = L.tileLayer('//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
                  maxZoom: 18,
                  attribution: '&copy; <a href="//openstreetmap.org/copyright">OpenStreetMap</a> contributors, Points &copy 2012 LINZ'
                });

                var map = L.map('map', {
                  center: L.latLng(-6.6, 106.42),
                  zoom: 5,
                  layers: [tiles]
                });

                var mcg = L.markerClusterGroup({
                  chunkedLoading: true,
                  //singleMarkerMode: true,
                  spiderfyOnMaxZoom: true
                });

                for (var i = 0; i < planelatlong.length; i++) {
                  marker = new L.marker([planelatlong[i][1],planelatlong[i][2]]);
                  marker.bindPopup("<?php print json_encode($rows); ?>");
                  mcg.addLayer(marker);
                  marker.on('click', onClick);
                }
                  function onClick(e) {
                    var popup = e.target.getPopup();
                    var content = popup.getContent();
                  }
                map.addLayer(mcg);

              </script>

Here is the "planelatlon.php" code :

<?php
    include "koneksi.php";
    $select = mysqli_query($konek,"SELECT id,lat,lon FROM data_gempa_jawa");

    if ( ! $select ) {
        echo mysqli_error();
        die;
    }

    $data = array();

    echo "var planelatlong = [";

    for ($x = 0; $x < mysqli_num_rows($select); $x++) {
        $data[] = mysqli_fetch_assoc($select);
        echo "[",$data[$x]['id'],",",$data[$x]['lat'],",",$data[$x]['lon'],"]";
        if ($x <= (mysqli_num_rows($select)-2) ) {
            echo ",";
        }
    }
        echo "];";

    mysqli_close($konek);
?>

Thanks a lot!

  • 写回答

1条回答 默认 最新

  • doutui4649 2019-08-15 16:56
    关注

    You can generate a geojson file from php and then process it like any other from leaflet.

    onEachFeature: function(feature, layer) {
            layer._leaflet_id = feature.properties.control;
    
            var popupText = feature.properties.description 
                layer.bindPopup(popupText)
                layer.bindTooltip(feature.properties.title).openTooltip(); 
                }
                });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配