Search code examples
javascriptarcgisesriarcgis-js-api

(ESRI) ReferenceError: Locator is not defined - Even though it is included


I'm at my wit's end as to why I'm getting the "not defined" error. I'm calling/including the dependencies as required. Below is the code that I'm using:

dojo.require("esri.map");
dojo.require("esri.tasks.locator");
dojo.require("esri.dijit.Geocoder");
.
.
.
var locator;
.
.
.
// this line is throwing error
locator = new Locator("http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer");

In the browser console, the following message is displayed:

ReferenceError: Locator is not defined

Why does this happen?


Solution

  • Short answer: use the full module name:

    locator = new esri.tasks.Locator("http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer");
    

    Long answer:

    You're using Dojo's legacy synchronous module loading (dojo.require) instead of the newer asynchronous module definition (AMD) loading (require). When you use legacy loading, you have to provide the full module name when you use a class.

    By default, the ArcGIS API for JavaScript documentation displays the AMD documentation because AMD is newer and faster, but each class has a link at the top of the page to switch to legacy module loading. For example:

    esri.tasks.Locator (AMD)

    There's a link at the top of that page that says Legacy Module Require. Click it and you get this:

    esri.tasks.Locator (legacy)

    That page tells you how to use Locator with legacy loading, including the need to say esri.tasks.Locator instead of just Locator when calling the constructor.

    By the way, I suggest that you change to AMD loading if you're able, especially if this is for a new project. But if you just want to get it working, change Locator to esri.tasks.Locator and you're all set.