AnsweredAssumed Answered

Having trouble updating attributes automatically with editor widget with values from a function

Question asked by christinawong on Sep 17, 2018

I'm creating a web app to edit some feature services and I'd like to auto populate some attribute fields with values based on the added/moved point's location. I have copied some code from a function that grabs the values from a SOAP service. The code was not written by me and is originally for a google maps web map but I figured out which parts of the code I needed.

 

I see the values for the attributes are there, but these values from the function are not being saved to the attribute fields. Everything else that's not using the function is updated consistently, in this case, the point_x, point_y attributes (thanks to Robert Scheitlin's help).

 

I'm not seeing any error messages in the developer console. The values sometimes populates but do not save when the map is refreshed or the point is clicked on again. I'm stuck and haven't been able to figure this out after searching online for solutions.

 

            dojo.forEach(featLayers, function(feature) {
                feature.on("before-apply-edits", updateCoordinates);
            });

            function updateCoordinates(evt) {
                var toAdd = evt.adds;
                var toUpdate = evt.updates;
               
                dojo.forEach(toAdd, function(add) {
                    add.attributes.point_y = add.geometry.getLatitude();
                    add.attributes.point_x = add.geometry.getLongitude();
                    getPostmileFromGeoCoords(add, function(fpm) {
                        add.attributes.odometer = fpm.odometer;
                        add.attributes.co = fpm.postmile.countyCode;
                        add.attributes.rte = fpm.postmile.routeNumber;
                        add.attributes.dir = fpm.postmile.alignmentCode;
                        add.attributes.pm_prefx = fpm.postmile.postmilePrefixCode;
                        add.attributes.pm = fpm.postmile.postmileValue;
                        console.log("Postmile: " + fpm.postmile.countyCode + "-" + fpm.postmile.routeNumber + "-" + fpm.postmile.postmilePrefixCode + fpm.postmile.postmileValue + " Odo: " + fpm.odometer);
                    });
                });
               
                dojo.forEach(toUpdate, function(update) {
                    if(update.geometry) {
                        update.attributes.point_y = update.geometry.getLatitude();
                        update.attributes.point_x = update.geometry.getLongitude();
                        getPostmileFromGeoCoords(update, function(fpm) {
                            update.attributes.odometer = fpm.odometer;
                            update.attributes.co = fpm.postmile.countyCode;
                            update.attributes.rte = fpm.postmile.routeNumber;
                            update.attributes.dir = fpm.postmile.alignmentCode;
                            update.attributes.pm_prefx = fpm.postmile.postmilePrefixCode;
                            update.attributes.pm = fpm.postmile.postmileValue;
                            console.log("Postmile: " + fpm.postmile.countyCode + "-" + fpm.postmile.routeNumber + "-" + fpm.postmile.postmilePrefixCode + fpm.postmile.postmileValue + " Odo: " + fpm.odometer);
                        });
                       
                    }
                });
            }

 

 

            // Determine the postmile closest to a given latitude and longitude
            function getPostmileFromGeoCoords(pt, callBackFunction) {
                pt.spatialReferenceID = srid.WGS84;
                var req = new revPMRequest();
                var opt = new revPMOptions();
                opt.toleranceDegrees = 0.01703; //~1 mile
                //req.inputPoint = pt;
                req.inputPoint = webMercatorUtils.webMercatorToGeographic(pt.geometry);
                req.options = opt;
                var ws = new GISWebServiceSoapImp();
                ws.getPostmileForPoint(req,
                    function (res) {
                        var pm = res.outputPostmile;
                        if (pm !== null) {
                            var fpm = new fleshedOutPm(pm);
                            getLatLong(fpm,
                                function (res) {
                                    fpm.latlng = res;
                                    if (fpm.latlng !== null) {
                                        getOdometer(fpm,
                                            function (res) {
                                                fpm.odometer = res;
                                                if (fpm.odometer !== null) {
                                                    callBackFunction(fpm);
                                                } else {
                                                    alert("Unable to determine the geo-coordinates for postmile "+pm.toString());
                                                }
                                            }
                                        );
                                    } else {
                                        alert("No Postmile found within 1 minute of lat,long (" + pt.y + "," +  pt.x + ")");
                                    }
                                }
                            );
                        } else {
                                alert("Unable to determine the statewide odometer for postmile "+pm.toString());
                        }
                    }
                );
            }
           
            function getLatLong(fpm, responseCallBack) {
                var options = new pmOptions();
                options.alignmentType = 0; //centerline

                if (fpm.postmile.alignmentCode === 'L')
                    options.alignmentType = 2;

                if (fpm.postmile.alignmentCode === 'R')
                    options.alignmentType = 3;

                options.effectiveDate = null;
                options.offsetDistance = 0.0;
                var req = new pmRequest();
                req.postmile = fpm.postmile;
                req.options = options;
                var ws = new GISWebServiceSoapImp();
                ws.getCoordinatesForPostmile(req,
                    function(res) {
                        var pt = null;
                        if (res !== null){
                            pt = res.pointGeometry;
                        }
                        responseCallBack(pt);
                    }
                );
            }
           
            function getOdometer(fpm, responseCallBack) {
                //web service call to get odometer
                var req = new pmToOdometerRequest();
                req.postmile = fpm.postmile;

                if (fpm.postmile.alignmentCode === 'L')
                    req.alignmentCode = 'L';
                else
                    req.alignmentCode = "R"; //just get one odometer - use the Right alignment

                var ws = new GISWebServiceSoapImp();
                ws.getOdometerForPostmile(req,
                    function(res) {
                        var odo = null;
                        if (res !== null) {
                            odo = res.odometer;
                        }
                        responseCallBack(odo);
                    }
                );
            }

 

Any help and insights is much appreciated.

 

Thanks!

Outcomes