Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="initial-scale=1,maximum-scale=1,user-scalable=no"
    />
    <title>offset and nearestCoordinate - 4.14</title>

    <link
      rel="stylesheet"
      href="https://js.arcgis.com/4.18/esri/themes/light/main.css"
    />
    <script src="https://js.arcgis.com/4.18/"></script>

    <style>
      html,
      body,
      #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
      }
    </style>

    <script>
      require([
        "esri/Map",
        "esri/views/MapView",
        "esri/layers/FeatureLayer",
        "esri/Graphic",
        "esri/geometry/geometryEngine",
        "esri/core/promiseUtils"
      ], function(Map, MapView, FeatureLayer, Graphic, geometryEngine, promiseUtils) {
        var map = new Map({
          basemap: "gray"
        });

        var view = new MapView({
          container: "viewDiv",
          map: map,
          highlightOptions: {
            color: [255, 255, 0, 1],
            haloOpacity: 0.9,
            fillOpacity: 0.2
          },
          zoom: 6, 
          center: [-100, 35]
        });

        var featureLayer = new FeatureLayer({
          url:
            "https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/1",
          renderer: {
            type: "simple",
            symbol: {
              type: "simple-line",
              color: "red",
              width: "2px"
            }
          },
          outFields: ["*"]
        });
        map.add(featureLayer);
        var featureLayer2;
        
        view.whenLayerView(featureLayer).then(function(lv){
          var query = featureLayer.createQuery();
          query.where = "type = 'Multi-Lane Divided'";
          query.outSpatialReference = view.spatialReference;
          featureLayer.queryFeatures(query).then(function(results){
            var originalGeometries =  results.features.map(function(feature){
              return feature.geometry;
            });
            
            var oset = (view.extent.width / view.width) * 2;
            var result = geometryEngine.offset(originalGeometries, oset, "meters");
            
            var offsetFeatures = results.features.map(function(feature, i){
              var feat  = new Graphic({
                geometry: result[i],
                attributes: feature.attributes
              })
              feature.geomery = result[i];
              return feat;
            });

            var fields = featureLayer.fields.slice(0,8);
            featureLayer2 = new FeatureLayer({
              source: offsetFeatures,
              geometryType: "polyline",
              fields: fields,
              renderer: {
                type: "simple",
                symbol: {
                  type: "simple-line",
                  color: "blue",
                  width: "2px"
                }
              },
              outFields: ["*"],
              spatialReference: view.spatialReference
            });

            map.add(featureLayer2);
          });
        });
        
        const updateStatistics = promiseUtils.debounce(function (event) {
          view.hitTest(event).then(function(response){
            if (view.graphics){
              view.graphics.removeAll();
            }
            if (response.results.length) {
              
              // for click event
              var mapPoint = event.mapPoint;
              
              // when listening to pointer-move event
              // var mapPoint = view.toMap({x: event.x, y: event.y});
              var graphic = response.results.map(function (result) {
                if (result.graphic.layer == featureLayer || result.graphic.layer == featureLayer2) {
                  return result.graphic;
                }
              });

              var near = geometryEngine.nearestVertex(graphic[0].geometry, mapPoint);
              
              var newPath = [];
              if (near.isRightSide) {
                newPath.push(graphic[0].geometry.paths[0][near.vertexIndex-1]);
                newPath.push(graphic[0].geometry.paths[0][near.vertexIndex]);
              } else {
                newPath.push(graphic[0].geometry.paths[0][near.vertexIndex]);
                newPath.push(graphic[0].geometry.paths[0][near.vertexIndex+1]);
              }
              
              var segGraphic = new Graphic({
                geometry: {
                  type: "polyline",
                  paths: [newPath],
                  spatialReference: view.spatialReference
                },
                symbol: {
                  type: "simple-line",
                  color: "green",
                  width: "10px"
                }
              });
              view.graphics.add(segGraphic);
            }
          });
        });
        view.on("click", updateStatistics);
      });
    </script>
  </head>

  <body>
    <div id="viewDiv"></div>
  </body>
</html>

              
            
!

CSS

              
                
              
            
!

JS

              
                
              
            
!
999px

Console