ds1379551 2016-11-30 12:31
浏览 724
已采纳

Leaflet标记弹出文本中的换行符

I have a form to associate some text to a marker on a Leaflet map:

<form action="map.php" method="POST">
 <section>
  <label>write some text here</label>
   <textarea id="text" rows="3" name="area"></textarea>
 </section>

 <input type="submit" value="show map" />
</form>

As you can see above, the content of the textarea is passed to the page map.php, where a map is shown. On the map, the place marker is shown, and a popup contains the text posted through the textarea (variable $text):

<?php
$text = htmlspecialchars($_POST["text"]);
?>

<center>
 <div id="map" class="embed-container"></div>
</center>

<script>
  var map = L.map('map').setView([46.13, 11.11], 9);
  L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
    {
      attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
      maxZoom: 17,
      minZoom: 9
    }).addTo(map);
</script>
<script>
  var icon = L.icon({
        iconUrl: 'img/gps.png',
        iconSize: [25, 25],
        });
  var marker = L.marker(['<?=$latitude;?>', '<?=$longitude;?>'], {icon: icon}).addTo(map);
  marker.bindPopup('<?=$text;?>');
</script>

The problem is that if I press ENTER while I write something in the textarea, nothing is passed to the popup marker. I tried with and such, but nothing works. They are shown in the popup text as strings, not as newlines.

Any ideas?

  • 写回答

2条回答 默认 最新

  • duan0530 2016-11-30 13:10
    关注

    The problem is that if I press ENTER while I write something in the textarea, nothing is passed to the popup marker.

    This is inaccurate. The HTML inside the popup is receiving a newline character.

    The problem is that the newline character is being displayed as collapsed whitespace. This is the default in HTML. Let me quote the documentation for the white-space CSS property, inherited by default by all HTML elements:

    normal

    • Sequences of whitespace are collapsed. Newline characters in the source are handled as other whitespace. Breaks lines as necessary to fill line boxes.

    HTML collapses whitespace characters, including tabs and line breaks (but excluding &nbsp;s).

    HTML and whitespace

    Text inside Leaflet popups simply follow the same rule. Replace your newlines with HTML <br> line breaks, or otherwise wrap every line in a block-level element like a <div>.

    Popups and newlines

    If you are using php, the easiest way is to use the nl2br function. This is not necessarily the best way, as it is very prone to allowing XSS attacks in the hands of inexperienced developers. Using htmlspecialchars will not help if you are outputting JavaScript strings, enclosed in quotes. For these cases I recommend using json_encode to provide the quotes and the quote scaping, i.e. var foo = <?= json_encode(nl2br(str)); ?>;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。