3

When I first load my HTML5 with ArcGIS JavaScript API 3.21, the click events work. When I refresh the web page, one of the click events stops working (the identify task) I have tried setting the click event in either the map.on("load"...) and the map.on("layers-add-result"...). It doesn't matter.

Will the map.on("load"...) conflict with the map.on("layers-add-result"...)? I am using ArcGISDyanicMapServiceLayer to load multiple map service layers.

On a page refresh in a web browser, why doesn't the map.on("layers-add-result"...) not run again? I set up an alert to see if it got there on a refresh.

Is it an asynchronous issue? I tried controlling the timing by using callbacks, but it didn't seem to make a difference.

Here's the beginning of my code:

    parser.parse();
//This sample may require a proxy page to handle communications with the ArcGIS Server services. You will need to
//replace the url below with the location of a proxy on your machine. See the 'Using the proxy page' help topic
//for details on setting up a proxy page.
esriConfig.defaults.io.proxyUrl = "/proxy/";
esriConfig.defaults.io.alwaysUseProxy = false;

esriConfig.defaults.geometryService = new GeometryService("https://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");

gsvc = new GeometryService("https://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");

var geometryService = new GeometryService("http://sampleserver5.arcgisonline.com/arcgis/rest/services/Utilities/Geometry/GeometryServer");

clickedPointsArr = [];
var activetext = "off";

//esriConfig.defaults.io.corsDetection = false;

undoManager = new UndoManager();

//hook up undo/redo buttons

$("#undo").on("click", function () {
    undoManager.undo();
});
$("#redo").on("click", function () {
    undoManager.redo();
});

// layer indexes in the map services
var indexPoint_StormwaterOuterZoom = 34;
var indexPoint_StormwaterMiddleZoom = 35;
var indexPoint_StormwaterCloseUpZoom = 36;
var indexPoint_ControlPts = 1;
var indexPoint_FiberPts = 9;
var indexPoint_ReferencePts = 11;
var indexPoint_SchoolBusStopsOSPI = 19;
var indexPoint_AvistaPoleStructure = 28;
var indexLine_PriorityArray = 13;
var indexLine_RoadSegments = 38;
var indexLine_MajorRdsCitySt = 40;
var indexLine_Waterlines = 44;
var indexLine_Railroad = 41;
var indexPoly_NoTrucks = 32;
var indexPoly_CountyBdry = 13;
var indexPoly_Parcels = 53;
var indexPoly_MuniBdry = 83;
var indexPoly_Waterbody = 27;

var aerialURL = "http://igis.spokanecounty.org/arcgis/rest/services/Imagery/CurrentImagery/MapServer";
var aerialZoomURL = "http://igis.spokanecounty.org/arcgis/rest/services/Imagery/1_564_Dynamic/MapServer";

var customExtentAndSR = new esri.geometry.Extent(2380210.76526192, 109581.35051693, 2582107.45873931, 404235.534501359, new esri.SpatialReference({ "wkid": 2926 }));
var countyLayer = new esri.layers.GraphicsLayer();
var aerialLayer = new esri.layers.ArcGISDynamicMapServiceLayer(aerialURL, {
    id: "aerialLayer",
    visible: false
});

var pointsURL = "http://igis.spokanecounty.org/arcgis/rest/services/PublicWorks/PublicWorksQueriesPoints/MapServer";
var linesURL = "http://igis.spokanecounty.org/arcgis/rest/services/PublicWorks/PublicWorksQueriesLines/MapServer";
var polygonsURL = "http://igis.spokanecounty.org/arcgis/rest/services/PublicWorks/PublicWorksQueriesPolygons/MapServer";

var aerialLayerZoom = new esri.layers.ArcGISDynamicMapServiceLayer(aerialZoomURL, {
    id: "aerialLayerZoom",
    visible: false
});

var tilesLayer = new esri.layers.ArcGISDynamicMapServiceLayer(" http://gis.spokanecounty.org/arcgis/rest/services/Engineering/SectionMosaic/MapServer", {
    //var tilesLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://GISMO3.spokanecounty.org:6080/arcgis/rest/services/Engineering/SectionMosaic/MapServer", {
    id: "tilesLayer",
    visible: false
});

var pointsLayer = new esri.layers.ArcGISDynamicMapServiceLayer(pointsURL, {
    id: "pointsLayer",
    visible: true
});
var linesLayer = new esri.layers.ArcGISDynamicMapServiceLayer(linesURL, {
    id: "linesLayer",
    visible: true
});
var polygonsLayer = new esri.layers.ArcGISDynamicMapServiceLayer(polygonsURL, {
    id: "polygonsLayer",
    visible: true
});

var connects;
var refPtsLyr = 11;
var parcelslLyr = 53;
var aerialLayerInfoID = 91;
var aerialLayerInfoIDText = '"' + aerialLayerInfoID + '"';
var sectionmapsLayerInfoID = 149;
var sectionmapsLayerInfoIDText = '"' + sectionmapsLayerInfoID + '"';
var SWstructuresLayerInfoID = 128;
polygonsLayer.setImageFormat("png32");
polygonsLayer.setImageTransparency(true);

//Default values for Draw Tool
var TransVal = 1;
var MyThickness = 3;
var MyFontSize = 12;
var red = 0;
var green = 0;
var blue = 0;
var newColor = [red, green, blue, TransVal]

var myPointsLayer = "Points";
var myLinesLayer = "Lines";
var myPolygonsLayer = "Polygons";
var defaultPoints = true;
var defaultLines = true;
var defaultPolygons = true;
var myPointsArray = [];
var myLinesArray = [];
var myPolygonsArray = [];

$.ajax({
    type: "POST",
    url: "RPWebService.asmx/GetFCLayerProfileVisibleLayers",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: '{"servicelayer":"' + myPointsLayer + '"}',
    async: false,
    success: function (data) {
        var obj = jQuery.parseJSON(data.d);
        if (obj == "Points NoResults;No Results Found") {
            defaultPoints = true;
            myPointsArray = [defaultVisiblePoints];
            //pointsLayer.setVisibleLayers([defaultVisiblePoints]);
        }
        else {
            defaultPoints = false;
            myPointsArray = [obj];
            //pointsLayer.setVisibleLayers([obj]);
        }
    },
    error: function (data) {
        alert('Unable to load Map Profile: ' + data.status + ' ' + data.statusText);
        myPointsArray = [defaultVisiblePoints];
        //pointsLayer.setVisibleLayers([defaultVisiblePoints]);
    }
});

$.ajax({
    type: "POST",
    url: "RPWebService.asmx/GetFCLayerProfileVisibleLayers",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: '{"servicelayer":"' + myLinesLayer + '"}',
    async: false,
    success: function (data) {
        var obj = jQuery.parseJSON(data.d);
        if (obj == "Lines NoResults;No Results Found") {
            defaultLines = true;
            myLinesArray = [defaultVisibleLines];
            //linesLayer.setVisibleLayers([defaultVisibleLines]);
        }
        else {
            defaultLines = false;
            myLinesArray = [obj];
            //linesLayer.setVisibleLayers([obj]);
        }
    },
    error: function (data) {
        alert('Unable to load Map Profile: ' + data.status + ' ' + data.statusText);
        defaultLines = true;
        myLinesArray = [defaultVisibleLines];
        //linesLayer.setVisibleLayers([defaultVisibleLines]);
    }
});

$.ajax({
    type: "POST",
    url: "RPWebService.asmx/GetFCLayerProfileVisibleLayers",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: '{"servicelayer":"' + myPolygonsLayer + '"}',
    async: false,
    success: function (data) {
        var obj = jQuery.parseJSON(data.d);
        if (obj == "Polygons NoResults;No Results Found") {
            defaultPolygons = true;
            myPolygonsArray = [defaultVisiblePolygons];
            //polygonsLayer.setVisibleLayers([defaultVisiblePolygons]);
        }
        else {
            defaultPolygons = true;
            myPolygonsArray = [obj];
            //polygonsLayer.setVisibleLayers([obj]);
        }
    },
    error: function (data) {
        alert('Unable to load Map Profile: ' + data.status + ' ' + data.statusText);
        defaultPolygons = true;
        myPolygonsArray = [defaultVisiblePolygons];
        //polygonsLayer.setVisibleLayers([defaultVisiblePolygons]);
    }
});

map = new Map("map", {
    extent: customExtentAndSR
});

//home button
var home = new HomeButton({
    map: map
}, "HomeButton");
home.startup();

//scalebar
var scalebar = new Scalebar({
    map: map
});

map.on("load", function () {
    toolbar = new Draw(map);
    toolbar.on("draw-end", addToMap);
    //toolbar.on("draw-complete", addMeas);
    editToolbar = new Edit(map);
    pointsLayer.setVisibleLayers([myPointsArray]);
    linesLayer.setVisibleLayers([myLinesArray]);
    polygonsLayer.setVisibleLayers([myPolygonsArray]);
    map.on("click", function (evt) {
        editToolbar.deactivate();
    });
    createMapMenu();
    createGraphicsMenu();
    map.reposition();
    map.resize();

    buildLayerList();
    firstPrint();
    // set up identify task click event
    connects = on(map, "click", executeIdentifyTask);
});

map.on("layers-add-result", function () {
    // test to see if sub-layers have been set as visible
    for (var j = 0; j < map.layerIds.length; j++) {
        var layer = map.getLayer(map.layerIds[j]);
        alert(layer.id + ' ' + layer.opacity + ' ' + layer.visible + ' ' + layer.visibleLayers);
    }
    // add legend
    legendDijit = new Legend({
        map: map,
        layerInfos: [{
            layer: polygonsLayer,
            hideLayers: [indexPoly_CountyBdry, indexPoly_MuniBdry],
            title: " ",
            respectCurrentMapScale: true
        },
        {
            layer: linesLayer,
            hideLayers: [indexLine_PriorityArray],
            title: " ",
            respectCurrentMapScale: true
        },
        {
            layer: pointsLayer,
            hideLayers: [indexPoint_ControlPts, indexPoint_FiberPts, indexPoint_ReferencePts, indexPoint_SchoolBusStopsOSPI],
            title: " ",
            respectCurrentMapScale: true
        }]
    }, "legendDiv");
    legendDijit.startup();
});

map.on("extent-change", function () {
    //alert("visible layers for points = " + pointsLayer.visibleLayers);
    var tempArray1, tempArray2, tempArray3 = [];
    tempArray1 = (getVisibleLayersPoints());
    if (tempArray1.length) {
        pointsLayer.setVisibleLayers(tempArray1);
    }                   
    tempArray2 = (getVisibleLayersLines());
    if (tempArray2.length) {
        linesLayer.setVisibleLayers(tempArray2);
    }
    tempArray3 = (getVisibleLayersPolygons());
    if (tempArray3.length) {
        polygonsLayer.setVisibleLayers(tempArray3);
    }
    $('#divScale').text("Scale: " +  number.format(map.getScale(), { places: 0 }));
});

map.addLayers([aerialLayer, aerialLayerZoom, tilesLayer, polygonsLayer, linesLayer, pointsLayer]);
  • Try putting the code starting from map.on("layers-add-result"... to map.addLayers(.... inside of map.on("load" – Aamir Suleman Jul 18 '17 at 13:50
  • Also could be a timing issue. Where is executeIdentifyTask defined? Is it before map.on("load.. if not, then that could be a possible reason. – Aamir Suleman Jul 18 '17 at 14:02
1

i think, you getting your ajax query's response after map onload so you need ajax query move to map onload function.

maybe do not add layer before map loading. i am not sure.

so finally code here :

    parser.parse();
//This sample may require a proxy page to handle communications with the ArcGIS Server services. You will need to
//replace the url below with the location of a proxy on your machine. See the 'Using the proxy page' help topic
//for details on setting up a proxy page.
esriConfig.defaults.io.proxyUrl = "/proxy/";
esriConfig.defaults.io.alwaysUseProxy = false;

esriConfig.defaults.geometryService = new GeometryService("https://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");

gsvc = new GeometryService("https://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");

var geometryService = new GeometryService("http://sampleserver5.arcgisonline.com/arcgis/rest/services/Utilities/Geometry/GeometryServer");

clickedPointsArr = [];
var activetext = "off";

//esriConfig.defaults.io.corsDetection = false;

undoManager = new UndoManager();

//hook up undo/redo buttons

$("#undo").on("click", function () {
    undoManager.undo();
});
$("#redo").on("click", function () {
    undoManager.redo();
});

// layer indexes in the map services
var indexPoint_StormwaterOuterZoom = 34;
var indexPoint_StormwaterMiddleZoom = 35;
var indexPoint_StormwaterCloseUpZoom = 36;
var indexPoint_ControlPts = 1;
var indexPoint_FiberPts = 9;
var indexPoint_ReferencePts = 11;
var indexPoint_SchoolBusStopsOSPI = 19;
var indexPoint_AvistaPoleStructure = 28;
var indexLine_PriorityArray = 13;
var indexLine_RoadSegments = 38;
var indexLine_MajorRdsCitySt = 40;
var indexLine_Waterlines = 44;
var indexLine_Railroad = 41;
var indexPoly_NoTrucks = 32;
var indexPoly_CountyBdry = 13;
var indexPoly_Parcels = 53;
var indexPoly_MuniBdry = 83;
var indexPoly_Waterbody = 27;

var aerialURL = "http://igis.spokanecounty.org/arcgis/rest/services/Imagery/CurrentImagery/MapServer";
var aerialZoomURL = "http://igis.spokanecounty.org/arcgis/rest/services/Imagery/1_564_Dynamic/MapServer";

var customExtentAndSR = new esri.geometry.Extent(2380210.76526192, 109581.35051693, 2582107.45873931, 404235.534501359, new esri.SpatialReference({ "wkid": 2926 }));
var countyLayer = new esri.layers.GraphicsLayer();
var aerialLayer = new esri.layers.ArcGISDynamicMapServiceLayer(aerialURL, {
    id: "aerialLayer",
    visible: false
});

var pointsURL = "http://igis.spokanecounty.org/arcgis/rest/services/PublicWorks/PublicWorksQueriesPoints/MapServer";
var linesURL = "http://igis.spokanecounty.org/arcgis/rest/services/PublicWorks/PublicWorksQueriesLines/MapServer";
var polygonsURL = "http://igis.spokanecounty.org/arcgis/rest/services/PublicWorks/PublicWorksQueriesPolygons/MapServer";

var aerialLayerZoom = new esri.layers.ArcGISDynamicMapServiceLayer(aerialZoomURL, {
    id: "aerialLayerZoom",
    visible: false
});

var tilesLayer = new esri.layers.ArcGISDynamicMapServiceLayer(" http://gis.spokanecounty.org/arcgis/rest/services/Engineering/SectionMosaic/MapServer", {
    //var tilesLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://GISMO3.spokanecounty.org:6080/arcgis/rest/services/Engineering/SectionMosaic/MapServer", {
    id: "tilesLayer",
    visible: false
});

pointsLayer = new esri.layers.ArcGISDynamicMapServiceLayer(pointsURL, {
    id: "pointsLayer",
    visible: true
});
linesLayer = new esri.layers.ArcGISDynamicMapServiceLayer(linesURL, {
    id: "linesLayer",
    visible: true
});
polygonsLayer = new esri.layers.ArcGISDynamicMapServiceLayer(polygonsURL, {
    id: "polygonsLayer",
    visible: true
});

var connects;
var refPtsLyr = 11;
var parcelslLyr = 53;
var aerialLayerInfoID = 91;
var aerialLayerInfoIDText = '"' + aerialLayerInfoID + '"';
var sectionmapsLayerInfoID = 149;
var sectionmapsLayerInfoIDText = '"' + sectionmapsLayerInfoID + '"';
var SWstructuresLayerInfoID = 128;
polygonsLayer.setImageFormat("png32");
polygonsLayer.setImageTransparency(true);

//Default values for Draw Tool
var TransVal = 1;
var MyThickness = 3;
var MyFontSize = 12;
var red = 0;
var green = 0;
var blue = 0;
var newColor = [red, green, blue, TransVal]

var myPointsLayer = "Points";
var myLinesLayer = "Lines";
var myPolygonsLayer = "Polygons";
var defaultPoints = true;
var defaultLines = true;
var defaultPolygons = true;
var myPointsArray = [];
var myLinesArray = [];
var myPolygonsArray = [];



map = new Map("map", {
    extent: customExtentAndSR
});

map.on("layers-add-result", function () {
    // test to see if sub-layers have been set as visible
    for (var j = 0; j < map.layerIds.length; j++) {
        var layer = map.getLayer(map.layerIds[j]);
        alert(layer.id + ' ' + layer.opacity + ' ' + layer.visible + ' ' + layer.visibleLayers);
    }
    // add legend
    legendDijit = new Legend({
        map: map,
        layerInfos: [{
            layer: polygonsLayer,
            hideLayers: [indexPoly_CountyBdry, indexPoly_MuniBdry],
            title: " ",
            respectCurrentMapScale: true
        },
        {
            layer: linesLayer,
            hideLayers: [indexLine_PriorityArray],
            title: " ",
            respectCurrentMapScale: true
        },
        {
            layer: pointsLayer,
            hideLayers: [indexPoint_ControlPts, indexPoint_FiberPts, indexPoint_ReferencePts, indexPoint_SchoolBusStopsOSPI],
            title: " ",
            respectCurrentMapScale: true
        }]
    }, "legendDiv");
    legendDijit.startup();
});

map.on("extent-change", function () {
    //alert("visible layers for points = " + pointsLayer.visibleLayers);
    var tempArray1, tempArray2, tempArray3 = [];
    tempArray1 = (getVisibleLayersPoints());
    if (tempArray1.length) {
        pointsLayer.setVisibleLayers(tempArray1);
    }                   
    tempArray2 = (getVisibleLayersLines());
    if (tempArray2.length) {
        linesLayer.setVisibleLayers(tempArray2);
    }
    tempArray3 = (getVisibleLayersPolygons());
    if (tempArray3.length) {
        polygonsLayer.setVisibleLayers(tempArray3);
    }
    $('#divScale').text("Scale: " +  number.format(map.getScale(), { places: 0 }));
});

map.on("load", function () {

    //home button
    var home = new HomeButton({
        map: map
    }, "HomeButton");
    home.startup();

    //scalebar
    var scalebar = new Scalebar({
        map: map
    });

    toolbar = new Draw(map);
    toolbar.on("draw-end", addToMap);
    //toolbar.on("draw-complete", addMeas);
    editToolbar = new Edit(map);
    map.on("click", function (evt) {
        editToolbar.deactivate();
    });
    createMapMenu();
    createGraphicsMenu();
    map.reposition();
    map.resize();

    buildLayerList();
    firstPrint();
    // set up identify task click event
    connects = on(map, "click", executeIdentifyTask);


    $.ajax({
        type: "POST",
        url: "RPWebService.asmx/GetFCLayerProfileVisibleLayers",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: '{"servicelayer":"' + myPointsLayer + '"}',
        async: false,
        success: function (data) {
            var obj = jQuery.parseJSON(data.d);
            if (obj == "Points NoResults;No Results Found") {
                defaultPoints = true;
                myPointsArray = [defaultVisiblePoints];
                pointsLayer.setVisibleLayers([defaultVisiblePoints]);
            }
            else {
                defaultPoints = false;
                myPointsArray = [obj];
                pointsLayer.setVisibleLayers([obj]);
            }
        },
        error: function (data) {
            alert('Unable to load Map Profile: ' + data.status + ' ' + data.statusText);
            myPointsArray = [defaultVisiblePoints];
            pointsLayer.setVisibleLayers([defaultVisiblePoints]);
        }
    });

    $.ajax({
        type: "POST",
        url: "RPWebService.asmx/GetFCLayerProfileVisibleLayers",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: '{"servicelayer":"' + myLinesLayer + '"}',
        async: false,
        success: function (data) {
            var obj = jQuery.parseJSON(data.d);
            if (obj == "Lines NoResults;No Results Found") {
                defaultLines = true;
                myLinesArray = [defaultVisibleLines];
                linesLayer.setVisibleLayers([defaultVisibleLines]);
            }
            else {
                defaultLines = false;
                myLinesArray = [obj];
                linesLayer.setVisibleLayers([obj]);
            }
        },
        error: function (data) {
            alert('Unable to load Map Profile: ' + data.status + ' ' + data.statusText);
            defaultLines = true;
            myLinesArray = [defaultVisibleLines];
            linesLayer.setVisibleLayers([defaultVisibleLines]);
        }
    });

    $.ajax({
        type: "POST",
        url: "RPWebService.asmx/GetFCLayerProfileVisibleLayers",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: '{"servicelayer":"' + myPolygonsLayer + '"}',
        async: false,
        success: function (data) {
            var obj = jQuery.parseJSON(data.d);
            if (obj == "Polygons NoResults;No Results Found") {
                defaultPolygons = true;
                myPolygonsArray = [defaultVisiblePolygons];
                polygonsLayer.setVisibleLayers([defaultVisiblePolygons]);
            }
            else {
                defaultPolygons = true;
                myPolygonsArray = [obj];
                polygonsLayer.setVisibleLayers([obj]);
            }
        },
        error: function (data) {
            alert('Unable to load Map Profile: ' + data.status + ' ' + data.statusText);
            defaultPolygons = true;
            myPolygonsArray = [defaultVisiblePolygons];
            polygonsLayer.setVisibleLayers([defaultVisiblePolygons]);
        }
    });

    map.addLayers([aerialLayer, aerialLayerZoom, tilesLayer, polygonsLayer, linesLayer, pointsLayer]);

});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.