Slight bug with Add Data widget regarding KML?

919
1
Jump to solution
02-12-2020 12:52 PM
EricRuberson1
New Contributor III

So one of my internal clients demo'd our project which uses the Add Data widget. They added a kml file but couldnt find where the layer went because it didnt zoom to the shape.

Whereas if you add a shapefile, it automatically zooms to whatever you added.

The 'bug' or rather, lacking automatic zoom feature, seems to be in the AddData widget's search/AddFromFilePane.js script.

The code that actually changes the map extent is in the _addFeatures function (line 260?)

But this is called outside of the logic for where a KML file is handled. It is only ever called if the file being loaded explicitely isn't a KML file.

It looks like line 455, where it checks if the projection for the added layer is okay, is probably the ideal spot to have the KML layer determine the extent.

So I'm going to try putting something there. If I come up with something that works, I'll add a response here.

Otherwise, I mostly just wanted to report the issue and see if anyone had already come up with a fix for this?

0 Kudos
1 Solution

Accepted Solutions
EricRuberson1
New Contributor III

This was my solution. I rewrote the if(projOk) statement that started on line 454, with optional comment code for logging the kml layer to console. If the user manages to upload a kml that isn't a point, line, or polygon, it will throw an error to the console and simply not zoom the map.

I should not that when I tested it with a point, it didnt zoom to the point, just panned to it. If you want it to zoom in to a point, you'll have to figure that one out on your own!

                  if (projOk) {
                    map.addLayer(lyr);
                    //console.log("AddFromFilePane.js| kml layer => ");
                    //console.log(lyr);
                    var newExtent;
                    var extentFound = false;
                    if (lyr._esriGeometryPolygon){
                      newExtent = lyr._esriGeometryPolygon.fullExtent;
                      extentFound = true;
                    }
                    if (lyr._esriGeometryPolyline){
                      newExtent = lyr._esriGeometryPolyline.fullExtent;
                      extentFound = true;
                    }
                    if (lyr._esriGeometryPoint){
                      newExtent = lyr._esriGeometryPoint.fullExtent;
                      extentFound = true;
                    }
                    if(extentFound === true){
                      map.setExtent(newExtent.expand(1.25),true);
                    }
                    else{
                      console.log("KML could not be processed into an ESRI geometry, so the map will not zoom to the loaded layer.");
                    }
                    
                  } else {
                    new Message({
                      titleLabel: i18n._widgetLabel,
                      message: i18n.addFromFile.kmlProjectionMismatch
                    });
                  }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

0 Kudos
1 Reply
EricRuberson1
New Contributor III

This was my solution. I rewrote the if(projOk) statement that started on line 454, with optional comment code for logging the kml layer to console. If the user manages to upload a kml that isn't a point, line, or polygon, it will throw an error to the console and simply not zoom the map.

I should not that when I tested it with a point, it didnt zoom to the point, just panned to it. If you want it to zoom in to a point, you'll have to figure that one out on your own!

                  if (projOk) {
                    map.addLayer(lyr);
                    //console.log("AddFromFilePane.js| kml layer => ");
                    //console.log(lyr);
                    var newExtent;
                    var extentFound = false;
                    if (lyr._esriGeometryPolygon){
                      newExtent = lyr._esriGeometryPolygon.fullExtent;
                      extentFound = true;
                    }
                    if (lyr._esriGeometryPolyline){
                      newExtent = lyr._esriGeometryPolyline.fullExtent;
                      extentFound = true;
                    }
                    if (lyr._esriGeometryPoint){
                      newExtent = lyr._esriGeometryPoint.fullExtent;
                      extentFound = true;
                    }
                    if(extentFound === true){
                      map.setExtent(newExtent.expand(1.25),true);
                    }
                    else{
                      console.log("KML could not be processed into an ESRI geometry, so the map will not zoom to the loaded layer.");
                    }
                    
                  } else {
                    new Message({
                      titleLabel: i18n._widgetLabel,
                      message: i18n.addFromFile.kmlProjectionMismatch
                    });
                  }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos