How to add FeatureLayer outside of initialization code

443
1
07-11-2017 05:53 AM
BjörnPaulström
New Contributor

How would i add a FeatureLayer from outside the initializing function?

example:

require(
[
    "esri/map",
    "esri/layers/FeatureLayer",
    "esri/dijit/PopupTemplate",
    "esri/geometry/Point",
    "esri/graphic",
    "dojo/domReady!"
],
function(Map, FeatureLayer, PopupTemplate, Point, Graphic)
{


    map = new Map("mapDiv", { basemap: "streets", center: [22, 65], zoom: 5 });
    getBasestationList();

});
});

function getBasestationList() {

    fromdatetime = fromdatetime.toLocaleString();
    todatetime = todatetime.toLocaleString();

    $.ajax(
    {
        type: "post",
        url: "{% url 'get_latest_basestation_list' %}",
        data:
        {
            "csrfmiddlewaretoken": "{{ csrf_token }}",
            "fromdate": fromdatetime,
            "todate": todatetime,
    },
    success: function (data, status)
    {
        featureLayer = new FeatureLayer(data, { id: 'basestations' });
        map.addLayers([featureLayer]);
    },
    error: function (request, status, error)
    {
        alert(request.responseText);
    }
});
};

However this gives me an error that FeatureLayer is not defined, if I do this in the intializing function it works, however I need to be able to add stuff to the map outside of the initializing function.

0 Kudos
1 Reply
ThomasSolow
Occasional Contributor III

FeatureLayer, and other Esri modules, will only be in scope of the callback that is passed into require (which is what I think you mean by initialization function).

You can define other functions within that function and then set up listeners/events so that your functions will fire based on user actions.