2

I've a Search widget on my map. What I'm trying to do is when a user types, let's say, 1215 Bourboun St, I want to append Sacramento, US to that value so that very precised results are returned back.

I've looked at the documentation and found a search-results event, which basically lets me play around with the returned results. But is there an event which I can subscribe to which takes place just before a search is done? This will enable me to modify the search value before initiating a search.

My code is as follows:

map = new Map("mapDiv", {
    center: [xyz, abc],
    zoom: 10,
    basemap: "streets"
});

var search = new Search({
   map: map       
}, dom.byId("search"));
search.startup();
search.on("search-results", populateHiddenField);
.
.
. 
// some irrelevant code

UPDATE

Using Gary's suggestion, I created a fiddle to let you experts help me out on this one.

Case Scenario:: If you start typing 1215, you'll notice that it returns result from all over the world and not just Sacramento even though I've extended the Search class per Gary's suggestion. Is there a way that I can restrict the autosuggestions to Sacramento alone?

JSFIDDLE

asprin
  • 9,579
  • 12
  • 66
  • 119

1 Answers1

1

UPDATE: the question was edited to enable suggestions instead of disabling them, and I have updated my answer accordingly. Handling suggestions is as simple as overriding the suggest method in exactly the same way as the search method.


Create a new class that extends Search and override the search function.

map = new Map("mapDiv", {
    center: [xyz, abc],
    zoom: 10,
    basemap: "streets"
});

//Define new class that inherits from Search
var MySearch = declare(Search, {
    //Override the search method
    search: function () {
        var originalValue = this.value;
        this.set("value", this.value + ", Sacramento, CA");
        var retVal = this.inherited(arguments);
        this.set("value", originalValue);
        return retVal;
    }

    //Override the suggest method
    suggest: function () {
        var originalValue = this.value;
        this.set("value", this.value + ", Sacramento, CA");
        var retVal = this.inherited(arguments);
        this.set("value", originalValue);
        return retVal;
    }
});           

var search = new MySearch({
    map: map,
    enableSuggestionsMenu:true,
    enableSuggestions:true,
    autoSelect:true,
    autoNavigate:true
}, dom.byId("search"));
search.startup();
search.on("search-results", populateHiddenField);

You'll need to include "dojo/_base/declare" in your require list.

See http://dojotoolkit.org/documentation/tutorials/1.10/declare/ for more details on extending a Dojo class.

Gary Sheppard
  • 4,764
  • 3
  • 25
  • 35