Add KML Layer Dynamically

459
2
Jump to solution
10-08-2019 07:04 AM
MKa
by
Occasional Contributor III

I am adding a KML Layer to my map from a URL which I need to retrieve asynchronously.  When I retrieve this URL, I attempt to add the KML Layer to an existing map, but it never seems to appear.  If I know the URL before the map loads, I am able to add the KMLLayer like normal, but when trying to add to a map that already exists, it never appears?

This Works on initial load of the map as expected

KmlLayer {
        KmlDataset {
            KmlGroundOverlay {
                id: groundOverlay
                KmlIcon {                   
                    url: kmlUri
                }
                Envelope {
                    id: envelopeCHI
                    xMax: kmlxMax
                    yMin: kmlyMin
                    xMin: kmlxMin
                    yMax: kmlyMax
                    spatialReference: SpatialReference.createWgs84()
                }
            }
        }
}

When trying to add dynamically to an already loaded map, nothing seems to be added?  I can even hardcode the url and envelope values with known URL and envelope and it still doesn't work.  

function addKMLLayer()
    {
        var newKmlIcon = ArcGISRuntimeEnvironment.createObject("KmlIcon");
        newKmlIcon.url = kmlUri

        var newEnvelope = ArcGISRuntimeEnvironment.createObject("Envelope");
        newEnvelope.id = envelopeCHI
        newEnvelope.xMax = kmlxMax
        newEnvelope.yMin = kmlyMin
        newEnvelope.xMin = kmlxMin
        newEnvelope.yMax = kmlyMax
        newEnvelope.spatialReference = SpatialReference.createWgs84()

        var newKmlGroundOverlay = ArcGISRuntimeEnvironment.createObject("KmlGroundOverlay", {newKmlIcon, newEnvelope });
        
        var newKMLDataset = ArcGISRuntimeEnvironment.createObject("KmlDataset", {newKmlGroundOverlay});

        var newKMLLayer = ArcGISRuntimeEnvironment.createObject("KmlLayer", {newKMLDataset});
        
        map.operationalLayers.append(newKMLLayer)
    }
0 Kudos
1 Solution

Accepted Solutions
LucasDanzinger
Esri Frequent Contributor

When you create a QML object directly and want to assign the properties dynamically, you have to explicitly set the properties as key-value pairs. In the declarative QML code, you don't need to explicitly set the property name, because it makes use of something called "default properties" in QML.

I rewrote your function and was able to get it working:

function addKMLLayer() {
    var newKmlIcon = ArcGISRuntimeEnvironment.createObject("KmlIcon", {
                                                               url: dataPath + "1944.jpg"
                                                           });

    var newEnvelope = ArcGISRuntimeEnvironment.createObject("Envelope", {
                                                                xMin: -123.066227926904,
                                                                yMin: 44.04736963555683,
                                                                xMax: -123.0796942287304,
                                                                yMax: 44.03878298600624,
                                                                spatialReference: SpatialReference.createWgs84()
                                                            });

    var newKmlGroundOverlay = ArcGISRuntimeEnvironment.createObject("KmlGroundOverlay", {icon: newKmlIcon, geometry: newEnvelope });

    var newKMLDataset = ArcGISRuntimeEnvironment.createObject("KmlDataset", {initRootNode: newKmlGroundOverlay});

    var newKMLLayer = ArcGISRuntimeEnvironment.createObject("KmlLayer", {dataset: newKMLDataset});

    map.operationalLayers.append(newKMLLayer)
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

2 Replies
LucasDanzinger
Esri Frequent Contributor

When you create a QML object directly and want to assign the properties dynamically, you have to explicitly set the properties as key-value pairs. In the declarative QML code, you don't need to explicitly set the property name, because it makes use of something called "default properties" in QML.

I rewrote your function and was able to get it working:

function addKMLLayer() {
    var newKmlIcon = ArcGISRuntimeEnvironment.createObject("KmlIcon", {
                                                               url: dataPath + "1944.jpg"
                                                           });

    var newEnvelope = ArcGISRuntimeEnvironment.createObject("Envelope", {
                                                                xMin: -123.066227926904,
                                                                yMin: 44.04736963555683,
                                                                xMax: -123.0796942287304,
                                                                yMax: 44.03878298600624,
                                                                spatialReference: SpatialReference.createWgs84()
                                                            });

    var newKmlGroundOverlay = ArcGISRuntimeEnvironment.createObject("KmlGroundOverlay", {icon: newKmlIcon, geometry: newEnvelope });

    var newKMLDataset = ArcGISRuntimeEnvironment.createObject("KmlDataset", {initRootNode: newKmlGroundOverlay});

    var newKMLLayer = ArcGISRuntimeEnvironment.createObject("KmlLayer", {dataset: newKMLDataset});

    map.operationalLayers.append(newKMLLayer)
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
MKa
by
Occasional Contributor III

That worked.  I was missing one thing that fixed it  initRootNode

        var newKMLDataset = ArcGISRuntimeEnvironment.createObject("KmlDataset", {initRootNode: newKmlGroundOverlay});
0 Kudos