Select to view content in your preferred language

Add AGO webmap operationalLayer JSON to an already created map object

1065
2
08-01-2013 11:09 AM
CaiZhiyuan
Deactivated User
Hoping someone can help.  I have a map where I use utils to createMap from a webmap JSON file.  Later, I want to add other layers that come from a config file that has all the properties of an operationalLayer.  Is there a way to add an operationalLayer to a map?

Currently, I'm creating a second map, grabbing the layers and then map.addLayer()-ing them into my current map.  While this works in some cases, larger more complex layers it hang/errors.

What I want is to be able to easily configure popups/visibility/opacity through JSON, and then as the user wants, bring these into the map.

Ideas? I'm struggling!
Thanks
0 Kudos
2 Replies
CaiZhiyuan
Deactivated User
Well, I answered my own question.  Here's some code direction for someone who might be doing the same thing.  I just create a layer for the mapservice, and then create feature layers for each layer inside.  This worked for me since all the layers I'm trying to dynamically add in are mapservices... I think in the future I'll need to adjust for other kinds of layers, but this works for now.

//create layer parameters based on JSON
                        var params = {
                            id: item.id,
                            visibility: item.visibility || true,
                            opacity: item.opacity || 1,
                            visibleLayers: item.visibleLayers || ["*"]
                        };
                        //create new dynamic layer based on JSON
                        var addingLayer = new esri.layers.ArcGISDynamicMapServiceLayer(item.url, params);
                        //title so that it gets added to custom TOC
                        addingLayer.title = item.title;
                        //Add the new layer
                        this.map.addLayer(addingLayer);

                        //Add the popups for the individual feature layers of the mapservice
                        if (item.layers) {
                            for (var j = 0; j < item.layers.length; j++) {
                                //create popup using popupinfo object already existing in JSON file
                                var pop = new esri.dijit.PopupTemplate(item.layers.popupInfo);
                                //create the feature layer and apply the popup template
                                var fL = new esri.layers.FeatureLayer(item.url + "/" + item.layers.id, {
                                    mode: 2,
                                    layerId: item.layers.id,
                                    id: item.id + "_" + item.layers.id,
                                    infoTemplate: pop,
                                    outFields: ["*"]
                                });
                                this.map.addLayer(fL);
                            }
                        };
0 Kudos
SusanHmel
Emerging Contributor
Jessie, thanks for your post and reply, it helped us figure out the same problem. We created a Dojo Memory store from a JSON file to get the PopupInfo based on what we built in an AGO webmap:

                    
                     if (layer.serviceInfo) {
                            for (var j = 0; j < layer.serviceInfo.layers.length; j++) {
                                //create popup using popupinfo object already existing in JSON file
                                var popupTemplateStore = new Memory({
                                    idProperty: "ID",
                                    data: json.parse(popupTemplate)
                                });
         
                                var TemplatefromJson = popupTemplateStore.get(layer.serviceInfo.layers.id);
                                var PopupTemplate = new esri.dijit.PopupTemplate({
                                    title: TemplatefromJson.popupInfo.title,
                                    fieldInfos: TemplatefromJson.popupInfo.fieldInfos,
                                    showAttachments: TemplatefromJson.popupInfo.showAttachments,
                                    description: TemplatefromJson.popupInfo.description,
                                    mediaInfos: TemplatefromJson.popupInfo.mediaInfos
                                })
                            
                                //create the feature layer and apply the popup template
                                var fL = new esri.layers.FeatureLayer(item.url + "/" + layer.serviceInfo.layers.id, {
                                    mode: 2,
                                    layerId: layer.serviceInfo.layers.id,
                                    id: item.id + "_" + layer.serviceInfo.layers.id,
                                    infoTemplate: PopupTemplate,
                                    outFields: ["*"]
                                });
                                this.map.addLayer(fL);
                                }
                        };
0 Kudos