I'm using the Search widget in the 4.9 version of the javascript API. Can I limit the results to a given country when using the default geocoding service?


You can define the locator source to the Search widget (in addition to using the default). So if you create a locator source, and define the world geocode service, it'll accept countryCode as a property: https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Search-LocatorSearchSource.html

Below I've added Locator, and done 2 things: 1) Created a new source to the world geocoder, setting the countryCode and 2) turned off the default search source (which is the world geocoder) with the includeDefaultSources property. If you dont turn it off, the user needs to use the drop down to use your locator.

 "esri/tasks/Locator"  >>  Locator

  var searchWidget = new Search({
    sources: [{
      locator: new Locator({ url: "https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer"}),
      singleLineFieldName: "SingleLine",
      name: "Custom Geocoding Service",
      localSearchOptions: {
        minScale: 300000,
        distance: 50000
      placeholder: "Search Canada",
      maxResults: 3,
      maxSuggestions: 6,
      suggestionsEnabled: true,
      minSuggestCharacters: 0
    view: view,
    includeDefaultSources: false

  // Add the search widget to the top right corner of the view
  view.ui.add(searchWidget, {
    position: "top-right"
  • Thanks! countryCode was what I was looking for. Is there an option for county as well?
    – RedHorse
    Oct 4 '18 at 14:24
  • The world geocoder certainly looks to support more filtering: developers.arcgis.com/rest/geocode/api-reference/… --although I'm not certain you can wire up the Search > Locator definition any more fine grain than "country". Maybe worth a try...
    – KHibma
    Oct 4 '18 at 14:32

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.