Hello, I'm trying to create an editing web map that will automatically update the coordinates of an added/moved point and also allow manual edits to other attributes. I was able to get the coordinates to update, based on this code I found on stack exchange.
Below is what I have in my HTML and seems to work whenever I add or move a point.
dojo.forEach(featLayers, function(feature) {
feature.on("before-apply-edits", updateCoordinates);
});
function updateCoordinates(evt) {
var toAdd = evt.adds;
dojo.forEach(toAdd, function(add) {
add.attributes.point_y = add.geometry.getLatitude();
add.attributes.point_x = add.geometry.getLongitude();
});
var toUpdate = evt.updates;
dojo.forEach(toUpdate, function(update) {
update.attributes.point_y = update.geometry.getLatitude();
update.attributes.point_x = update.geometry.getLongitude();
});
}
However, when I manually type in text for another attribute that is entered during the editing, I get these error messages in the console as shown in the attached graphic. Line #305 in the console refers to the above code line #10 update.attributes.point_y = update.geometry.getLatitude();
The attributes I manually update are not saved most of the time.
Any help is much appreciated!
Solved! Go to Solution.
Christina,
You just simply need to check if the update has geometry before you try and get the lat and lon.
function updateCoordinates(evt) {
var toAdd = evt.adds;
dojo.forEach(toAdd, function(add) {
add.attributes.point_y = add.geometry.getLatitude();
add.attributes.point_x = add.geometry.getLongitude();
});
var toUpdate = evt.updates;
dojo.forEach(toUpdate, function(update) {
if(update.geometry){
update.attributes.point_y = update.geometry.getLatitude();
update.attributes.point_x = update.geometry.getLongitude();
}
});
}
Christina,
You just simply need to check if the update has geometry before you try and get the lat and lon.
function updateCoordinates(evt) {
var toAdd = evt.adds;
dojo.forEach(toAdd, function(add) {
add.attributes.point_y = add.geometry.getLatitude();
add.attributes.point_x = add.geometry.getLongitude();
});
var toUpdate = evt.updates;
dojo.forEach(toUpdate, function(update) {
if(update.geometry){
update.attributes.point_y = update.geometry.getLatitude();
update.attributes.point_x = update.geometry.getLongitude();
}
});
}
Hi Robert,
Wow! I did what you suggested and everything's working out great now.
Thank you so much!