2

I've been working with ESRI's javascript API for about a week and finding that somethings click while other things are still a mystery to me. I've already created a simple web map that loads an editable feature service from my local installation of ArcGIS Server. Using the Editor module I've successfully created an interface that lets a user click on a point to see the popup with editable attributes, and new points can be created or deleted. Editing, moving, and deleting the points all show up back in my feature service MXD, so that's great. What I want to know how to do is have the API automatically write the X,Y/Lat/Long/coordinates of a point to the attribute table of my feature service. So for example when the Editor template is used to add a new point to the web map, have the script write the location of the point to the table. If the point is moved, update the table to reflect that new location. I've played with the map.on("mouse-move", showCoordinatesFunction) so that I can see the current position of my mouse cursor, but I need help getting that written to the table. Thanks for any help you can offer! I'll post the script I'm working with below.

<script src="http://js.arcgis.com/3.7/"></script>
  <script>
    var map;

    require([
      "esri/map",
      "esri/graphic",
      "esri/dijit/editing/Editor",
      "esri/layers/ArcGISDynamicMapServiceLayer",
      "esri/layers/FeatureLayer",
      "esri/dijit/Popup",
      "esri/dijit/PopupTemplate",
      "esri/tasks/GeometryService",

      "dojo/parser",
      "dojo/_base/array",
      "dojo/dom",
      "esri/arcgis/utils",
      "esri/domUtils",
      "dojo/dom-style",
      "dojo/dom-class",
      "dijit/registry",
      "dojo/dom-construct",

      "dijit/layout/BorderContainer", 
      "dijit/layout/ContentPane", 
      "dojo/domReady!"
    ], function(
      Map, Graphic, Editor,
      ArcGISDynamicMapServiceLayer, FeatureLayer,
      Popup, PopupTemplate, GeometryService,
      parser, arrayUtils, dom, arcgisUtils, domUtils, 
      domStyle, domClass, registry, domConstruct
    ) {
      parser.parse();

      esriConfig.defaults.geometryService = new GeometryService("http://localhost:6080/arcgis/rest/services/Utilities/Geometry/GeometryServer");

      map = new Map("map", {
        basemap: "satellite",
        center: [-112.06498, 33.509384],
        zoom: 17,

      });


      map.on("layers-add-result", initEditor);
      map.on("layers-add-result", showCoordinates);


      var myTestPointFL = new FeatureLayer("http://localhost:6080/arcgis/rest/services/Layers/KP_TestEditing/FeatureServer/0", {
        mode: esri.layers.FeatureLayer.MODE_ONDEMAND,
        outFields: ["*"],
        // infoTemplate: popupTemplate
      });
      map.addLayers([myTestPointFL]);


      // map.infoWindow.resize(400, 300);

      function initEditor(evt) {
        var layers = arrayUtils.map(evt.layers, function(result) {
          return { featureLayer: result.layer };

        });
        var mylayerInfos = [{
          'featureLayer': myTestPointFL,
          'showAttachments': false,
          'isEditable': true,
          'fieldInfos': [
        {'fieldName': 'PIN', 'visible': true, 'label':'PIN: '},
        {'fieldName': "APN", 'visible':true, 'label':"APN: "},
        {'fieldName': "CITY", 'visible':true, 'label':"City: "},
        {'fieldName': "ZIP", 'visible':true, 'label':"Zip: "},
        {'fieldName': "QS", 'visible':true, 'label':"QS: "},
        {'fieldName': "LAT", 'visible':true, 'label':"LAT: "},
        {'fieldName': "LONG_", 'visible':true, 'label':"LONG: "},
        {'fieldName': "X", 'visible':true, 'label':"X: "},
        {'fieldName': "Y", 'visible':true, 'label':"Y: "},
        ]           
        }];

        map.on("mouse-move", showCoordinates);
        map.on("mouse-drag", showCoordinates);

        var settings = {
          map: map,
          layerInfos: mylayerInfos,
        };
        var params = {
          settings: settings
        };
        var editorWidget = new Editor(params, 'editorDiv');
        editorWidget.startup();
      };

      function showCoordinates(evt) {
        console.log("show");
        var mp = esri.geometry.webMercatorToGeographic(evt.mapPoint);
        dojo.byId("info").innerHTML = mp.x.toFixed(6) + ", " + mp.y.toFixed(6);

      }

    });
    </script>

Update: User Jaq posted the pre-AMD style code that was exactly what I needed to set me on the right path. I'm going to include the updated code that I adapted for my script.

  myFeatureLayer.on("before-apply-edits", function(evt) {
  var toUpdate = evt.updates;
  dojo.forEach(toUpdate, function(update) {
    //debugger;
    update.attributes.LAT = update.geometry.getLatitude();
    update.attributes.LONG_ = update.geometry.getLongitude();
    });
  });
  • From what I can tell wouldn't you need to upon each instance find some kind of event handler that can capture the coordinates and then create a new point object to add/push/append to your featurelayer which you would post to the server? Kind of depends on the permissions set for that feature layer also. – Carl Carlson Oct 23 '13 at 19:35
  • this is not an answer, But a question to user18412 and Devdatta Tengshe. I am trying to do the same thing, (adding coords to points that are created using web app on arcgis online..featurelayer exists on my local server, web app using local service). how do i add javascript code to wep app? thanks rbc0905 – rbc0905 Sep 16 '14 at 16:17
  • You need to start your own question. – ianbroad Sep 16 '14 at 16:34
2

Check out the FeatureLayer's onBeforeApplyEdits event. You should be able to get the updated feature, then edit whatever attributes you need before it gets saved.

Feature Layer - onBeforeApplyEdits

dojo.connect(monumentFL, "onBeforeApplyEdits", function(
    // updates
    dojo.forEach(u, function (update) {
        debugger;
        update.attributes.LATITUDE = update.geometry.getLatitude();
        update.attributes.LONGITUDE = update.geometry.getLongitude();
    });
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.