1

I am trying to add a marker to the left/right of a point I have from a GeoJson object. I am trying to take my coord and subtract 0.00001 from it, but it errors out my code when I try to run it. I figured if it is essentially a JSON obj, I would be able to pull it and do math on it and save it as another variable. Any ideas?

code:

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.css" />
    <script src="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.js"></script>
    <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>

    <style>
      #my-map {
        width: 1700px;
        height: 900px;
      }
    </style>
  </head>

  <body>
    <div id="my-map"></div>
    <script>
      window.onload = function () {
        var basemap = L.tileLayer("http://{s}.tile.osm.org/{z}/{x}/{y}.png", {
          maxZoom: 19,
          minZoom: 5,
        });

        $.getJSON("geocode-output.geojson", function (data) {
          var geojson = L.geoJson(data, {
            onEachFeature: function (feature, layer) {
              layer.bindPopup(
                feature.properties.address +
                  "<p><b> Location Address: " +
                  feature.properties.address +
                  "</b></p>" +
                  "<p><b> Crit Flow: " +
                  feature.properties.crit_flow +
                  "</b></p>" +
                  "<p><b> Peak Hr: " +
                  feature.properties.peak_hr +
                  "</b></p>" +
                  "<p><b> Lat_test: " +
                  feature.geometry.coordinates[0] +
                  "</b></p>"
              );
            },
          });

          var googleSat = L.tileLayer("http://{s}.google.com/vt/lyrs=s&x={x}&y={y}&z={z}", {
            maxZoom: 20,
            subdomains: ["mt0", "mt1", "mt2", "mt3"],
          });

          var map = L.map("my-map").fitBounds(geojson.getBounds());
          //    .setView([0.0,-10.0], 2);

          let lat_test = feature.geometry.coordinates[0] - 0.00001;
          let lng_test = feature.geometry.coordinates[1];
          var marker = L.marker([37.7858, -122.401], { title: "My marker" }).addTo(map);

          basemap.addTo(map);
          googleSat.addTo(map);

          geojson.addTo(map);
        });
      };
    </script>
  </body>
</html>

Code output without arithmetic: Code output without arithmetic

Community
  • 1
  • 1
  • my GeoJSON looks like this: { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { "type": "Point", "coordinates": [ -94.5760059,39.238053 ] }, "properties": { "FIELD1":"0", "address":"N OAK TRFY at NE 79 TH TERR", "peak_hr":1645, "crit_flow":1.3073684210526315 } } ] } – Jack Lindsay May 19 '20 at 04:09
  • 1
    On first sight it looks like a [scope](https://developer.mozilla.org/en-US/docs/Glossary/Scope) problem, since you're referring to `feature` in a place where it hasn't been defined. – IvanSanchez May 19 '20 at 04:20
  • It works fine when I call it in the GeoJSON variable. All of the queries work and display data except the arithmetic part. I will add a screen shot of how it looks with the arithmetic commented out. – Jack Lindsay May 19 '20 at 04:23
  • What's the error? – peeebeee May 19 '20 at 08:29
  • It's not clear to me whether you want to perform arithmetic on a specific feature of the `FeatureCollection` (either by its index or checking a condition) or in every feature in the `FeatureCollection`. – IvanSanchez May 19 '20 at 10:06
  • I dont know the error message, I am running a simple python server on my pc and when I go to my local host with the code open, it gives me a blank page with no map or points. @IvanSanchez I am getting a coordinate so I want to take the lat from that coord which should be feature.geometry.coordinates[0] and I want to subtract .00001 from it so it creates a new point a few meters to the left. – Jack Lindsay May 19 '20 at 14:21
  • If you don't know the error message, then you should start with https://developer.mozilla.org/en-US/docs/Tools/Debugger – IvanSanchez May 19 '20 at 21:32

0 Answers0