javascriptpopupleafletesriweb-feature-service

Leaflet Popup GetContent (Convert meters to feet)


I am creating an app using leaflet and I have near zero javascript experience so bear with me...

I have the leaflet map up and running and have a web feature service containing USGS stream flow data however all of the information is in meters and I want to convert it to feet and show the result in my popup.

<html>
<head>
  <meta charset=utf-8 />
  <title>Leaflet Map</title>
  <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />

  <!-- Load Leaflet from CDN-->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/leaflet/1.0.0-rc.1/leaflet.css" />
  <script src="https://cdn.jsdelivr.net/leaflet/1.0.0-rc.1/leaflet-src.js"></script>

  <!-- Load Esri Leaflet from CDN -->
  <script src="https://cdn.jsdelivr.net/leaflet.esri/2.0.0/esri-leaflet.js"></script>

  <style>
    body { margin:0; padding:0; }
    #map { position: absolute; top:0; bottom:0; right:0; left:0; }
  </style>
</head>
<body>

<div id="map"></div>

<script>
  var map = L.map("map").setView([37.75, -122.23], 10);


  L.esri.basemapLayer("Topographic").addTo(map);
  var gauges = L.esri.featureLayer({
    url: 'https://services.arcgis.com/lGOekm0RsNxYnT3j/arcgis/rest/services/north_america_surface_water_values/FeatureServer/4'
  }).addTo(map);

  gauges.bindPopup(function(evt){
    return L.Util.template('<p>Current Level:{LEVEL_CURRENT} </p>', evt.feature.properties);
  });
</script>

</body>
</html

I read the Leaflet documentation and it mentioned something about getContent...I know this is probably very easy but I am a Python guy and know nothing about JS so even a push in the right direction would be great!

Thanks for the help!


Solution

  • You could try calculating the conversion, storing that as an object and using that to render to your template, or you could just append a current_level in feat as an additional property on your features property object.

    I think the first option maybe easier, in your case since you only seem to care about the one property.

    gauges.bindPopup(function(evt){
      var current_level = {
        in_feet: parseInt(evt.feature.properties.LEVEL_CURRENT*3.28084)
      };
      return L.Util.template('<p>Current Level: {in_feet}</p>', current_level);
    });