0

I'm trying to visualize a spot with a marker with leaflet in my application. For this I'm building a feature, which is working on a test website (https://embed.plnkr.co/plunk/iiGl5i)

[{"type":"Feature","geometry":{"type":"Point","coordinates":[30.0,34.0]},"properties":{"divesite_id":8,"name":"Cyprus, Larnaca, Zenobia","show_on_map":true}}]

The integration with leaflet works, if I'm using L.marker and addTo(map), but it doesn't with the geojson.

application.html.erb

<link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css"
   integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
   crossorigin=""/>
   <script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js"
   integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew=="
   crossorigin=""></script>

   <style>
     #map {height: 400px}
   </style>

_form.html.erb

<div id=map></div>
<script>
  var lng = '<%= @divesite.longitude %>';
  var lat = '<%= @divesite.latitude %>';
  var popup = '<b><%= @divesite.name %></b><br><%= @divesite.description %>';
  var geojson = '<%= @geojson %>';
  var map = L.map('map').setView([lat, lng], 5);
  L.tileLayer('https://api.maptiler.com/maps/topo/{z}/{x}/{y}.png?key=WuMlitiPNaB5uEkyEiZc').addTo(map);
  // var marker1 = L.marker([lat, lng]).addTo(map);
  // marker1.bindPopup(popup);

  function onEachFeature(feature, layer) {
      var popupContent = "<p> "+feature.properties.name + "</p>";

      if (feature.properties && feature.properties.popupContent) {
        popupContent += feature.properties.popupContent;
      }

      layer.bindPopup(popupContent);
    };

 L.geoJson(geojson,{onEachFeature: onEachFeature}).addTo(map);

</script>

I tried several ways to code I found, but can't find make it work. Please help :)

Christian K.
  • 107
  • 1
  • 5
  • are you sure that the geojson var is not empty? `console.log(geojson)` – Falke Design Mar 18 '20 at 19:57
  • Yes, I am :) made it visible with an alert: Geojson [{"type":"Feature","geometry":{"type":"Point","coordinates":[30.0,34.0]},"properties":{"divesite_id":8,"name":"Cyprus, Larnaca, Zenobia","show_on_map":true}}] – Christian K. Mar 19 '20 at 08:42

2 Answers2

2

Your geojson is a string, you have to parse it to a object.

var hospitals = JSON.parse(geojson):
L.geoJson(hospitals ,{onEachFeature: onEachFeature}).addTo(map);

Update

To decode the html you can use this: (thx to @ghybs)

function htmlDecode(input) {
  var doc = new DOMParser().parseFromString(input, "text/html");
  return doc.documentElement.textContent;
}
var hospitals = JSON.parse(htmlDecode(geojson)):
L.geoJson(hospitals ,{onEachFeature: onEachFeature}).addTo(map);

https://stackoverflow.com/a/34064434/8283938

Falke Design
  • 10,635
  • 3
  • 15
  • 30
0

So, this is working :)

function onEachFeature(feature, layer) {
       var popupContent = "<p> "+feature.properties.name + "</p>";

       if (feature.properties && feature.properties.popupContent) {
         popupContent += feature.properties.popupContent;
       }

       layer.bindPopup(popupContent);
     };

   function htmlDecode(input) {
     var doc = new DOMParser().parseFromString(input, "text/html");
     return doc.documentElement.textContent;
   };

   var sites = JSON.parse(htmlDecode('<%= @geojson %>'));
   L.geoJson(sites ,{onEachFeature: onEachFeature}).addTo(map);

I just fixed a few typos - thanks for help @Falke Design and @ghybs

Christian K.
  • 107
  • 1
  • 5