Add layer via URL (JS API)

1624
12
Jump to solution
06-09-2017 03:37 PM
ChristopherSchreiber
Occasional Contributor II

Hello all!

I am attempting to add a layer via a URL that the user provides via a text box in the UI. 

I can capture the layer URL and work with it in JS, but when I attempt to use it to create a layer, I get the following error in the console "Uncaught TypeError: a.hasChildNodes is not a function"

Here is the JS code that attempts to use the entered URL to create a new Dynamic Map Service Layer:

NOTE: The URL I have been working with is: nowcoast/mapoverlays_political (MapServer) 

        var layerURLbox = registry.byId("LayerURLbox");
        on(dom.byId("layerSubmit"), "click", getValue);

        function getValue() {
            console.log(layerURLbox.get("value"));
            layerValue = layerURLbox.get("value")
            getLayerType(layerValue);
        }

        function getLayerType(layerURLValue) {

            if (layerURLValue.indexOf("MapServer")) {
                console.log("Map Service");
                var layer = new ArcGISDynamicMapServiceLayer(layerURLValue, {
                    id: "Test"
                })
                map.addLayer(layer);
            }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The console error I mentioned occurs on line 14, where I attempt to create the new ArcGISDynamicMapServiceLayer.

It does not make it to map.addLayer.

If anyone has any pointers or has tried this before, please let me know!

Thank you!

Chris

Reply
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Honored Contributor

Make sure the arguments in the require function match the order of the modules that are loaded.

View solution in original post

12 Replies
mfcallahan
Occasional Contributor

Do you have the "esri/layers/ArcGISDynamicMapServiceLayer" module loaded?

ChristopherSchreiber
Occasional Contributor II

Yes I do load that module.

Reply
0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Chris,

   Have you console.logged the layerURLValue to see if it is a good complete url? Can you share more of your code?

Reply
0 Kudos
ChristopherSchreiber
Occasional Contributor II

I have, and it returns a valid Rest URL that I can open in a new tab. 

I will post more code as soon as I am able to.

Thanks!

Chris

Reply
0 Kudos
KenBuja
MVP Honored Contributor

Make sure the arguments in the require function match the order of the modules that are loaded.

View solution in original post

JordanBaumgardner
Occasional Contributor III

Make sure it's the right "Type". You are always using an ArcGISDynamicMapServiceLayer - Does that match your URL? or are you hitting a /MapService or an /ImageService?

Reply
0 Kudos
ChristopherSchreiber
Occasional Contributor II

Thank you all, 

It turns out I was loading the modules in the incorrect order.

Such a headache over such a simple mistake! haha

Chris

AndrewValenski__IT_
Occasional Contributor III

Hey Christopher (Christopher Schreiber‌)!

Would you be able to share your code? I'm trying to get ahead of the curve and migrate to the 4.X API but, given the lack of support for the 'Add Data Widget' in Esri 4.x, replicating the process has been a pain-point. All I'm looking for is the ability for users to add their own data via REST URLs! Seems so simple, right?!?

Any information / code / insight is appreciated!

Reply
0 Kudos
ChristopherSchreiber
Occasional Contributor II

Hello Andrew!

Sorry for the delayed reserpine. I have been working on this on the side and have had to fix some issues. I will post what I have soon. 

The code that I have utilizes the 3.x JS API.

Chris

Reply
0 Kudos