[OSM-dev] More points on Leaflet map

"Marc Schütz" schuetzm at gmx.net
Sun Apr 13 10:40:37 UTC 2014

>        var marker = L.marker([<? echo $dati[1][5]; ?>, <? echo $dati[1][6]; ?>]).addTo(map);
>        marker.bindPopup("<b><? echo $dati[1][2]; ?></b><br><? echo $dati[1][7]; ?>").openPopup();
> ...
> as suggested in Leaflet Quick Start Guide. If I want to show 50 points (stored in $dati matrix), what code can I
> use? I realized if the description to show in the popup is  long, Leaflet don't creates the map, how can I solve
> this problem?

(Please disregard the previous mail, I forgot to actually use json_decode() in the example.)

Whenever you output data that may contain characters with a special meaning, i.e. quotation marks, HTML angle brackets and the like, you have to make sure they are properly escaped for the language they will be embedded in. For HTML, you have to use `htmlspecialchars(...)` for that purpose; in this case, your data ends up in Javascript, so you have to use `json_encode(...)`.

The reason the map sometimes isn't created probably has nothing to do with the length of the description, but with the lack of quoting. Most likely, your texts contain a quotation mark, which results in JS code like this:

    marker.bindPopup("<b>some text</b><br>some "other" other").openPopup();

This is, of course, syntactically invalid.

Apart from that, it should be straight-forward:

    var marker;
    <?php foreach($dati as $d) { ?>
    marker = L.marker([<?php echo json_encode($d[5]); ?>, <?php echo json_encode($d[6]); ?>]).addTo(map);
    marker.bindPopup("<b>"+<?php echo json_encode($d[2]); ?>+"</b><br>"+
                     <?php echo json_encode($d[7]); ?>).openPopup();
    <?php } ?>

More information about the dev mailing list