JS API Search Widget Lat Long

1029
3
03-23-2020 10:59 AM
Shane_
by
New Contributor II

Hi all,

How can I get the 4.14 JS API search widget to respond to lat/long searches e.g. 33.667, -85.83065 without

includeDefaultSources: true

I don't want the users to be able to search for place names or metadata other than in sources I explicitly specify. Here's the code that responds to lat/long searches but gives the user many possibilities for searching:

 

var searchWidget = new Search({ 
view: view, 
allPlaceholder: "Search here", 
locationEnabled: false, 
includeDefaultSources: true,
maxSuggestions: 200, 
sources: [
 {
 layer: lyr_PostOffices,
 searchFields: ["Province", "County", "District", "Street"],
 suggestionTemplate: "{Province}, {County}, {District}, {Street}",
 exactMatch: false,
 outFields: ["*"],
 name: "Search only for Post Offices",
 placeholder: "Search for a post office",
 },
 {
 layer: lyr_CarParks,
 searchFields: ["Company", "Name"],
 suggestionTemplate: "{Company}, {Name}",
 exactMatch: false,
 outFields: ["*"],
 name: "Search only for Car Parks",
 placeholder: "Search for a car park",
 },
 ],
 });‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

 

Thanks,

Shane 

3 Replies
Noah-Sager
Esri Regular Contributor

So you want users to be able to search for place names in the two layers you defined in the search widget constructor, and be able to do reverse geocoding (e.g. user can supply lat/longs)?

The Esri World Geocoding Service (the default source) allows for reverse geocoding. So if you want the user to be able to do reverse geocoding, then I think you either have to allow default sources, or consume a different LocatorSearchSource (e.g. a service that does not have POI information). You might be able to publish this kind of service from ArcGIS Pro, or configure the properties in an ArcGIS Portal locator.

0 Kudos
Shane_
by
New Contributor II

Hi Noah,

Actually all I want to happen when the user enters a lat/long is for the map to be centered on that point and then zoom in. Would I need a reverse geocoding service for that or just listen for the click(?) event?

Thanks,

Shane

0 Kudos
Noah-Sager
Esri Regular Contributor

Well, you need some source that can take a lat/long input and find that location. Couple options to consider:

1) Little hacky, but include Esri World Geocoding Service as a source, but turn suggestions off. It can still search for POIs etc., but will at least not show them as suggestions. Example: https://codepen.io/noash/pen/dyogyLq?editors=1000 

2) Build a separate search box using the Locator task and/or the reverseGeocode endpoint. This means there would be 2 search boxes, one for the Search widget and the layer sources, and one for lat/long input.

Locator task: Locator | ArcGIS API for JavaScript 4.14 

reverseGeocode: reverseGeocode—ArcGIS REST API: World Geocoding Service | ArcGIS for Developers 

0 Kudos