AnsweredAssumed Answered

Custom popup disappearing on different sourceindex search.

Question asked by steve.peralta_COLeg on Apr 4, 2018

I have three sources for a search. Default is "all." On initial locator search with my custom popup, it shows fine. When I switch search to a different feature layer sourceindex and then switch back to locator search, the custom popup disappears. Here is the code (please excuse messiness as I'm still learning the API):

 

// Search widget
let searchWidget = new Search({
view: view,
sources: [{
locator: new Locator({
url: locatorServiceUrl
}),
singleLineFieldName: "SingleLine",
name: "Place",
localSearchOptions: {
minScale: 300000,
distance: 50000
},
placeholder: "Search Places",
maxResults: 3,
maxSuggestions: 6,
suggestionsEnabled: true,
popupEnabled: false,
minSuggestCharacters: 0
}, {
featureLayer: houseLyr,
searchFields: ["LAST_NAME", "DISTRICT"],
displayField: "LAST_NAME",
exactMatch: false,
outFields: ["*"],
name: "House Members",
maxResults: 10,
maxSuggestions: 10,
suggestionsEnabled: true,
minSuggestCharacters: 0,
placeholder: "District No. or Last Name"
}, {
featureLayer: senateLyr,
searchFields: ["LAST_NAME", "DISTRICT"],
displayField: "LAST_NAME",
exactMatch: false,
outFields: ["*"],
name: "Senate Members",
placeholder: "District No. or Last Name",
maxResults: 6,
maxSuggestions: 6,
suggestionsEnabled: true,
minSuggestCharacters: 0
}]
});

//LAYERS QUERY
function getQuery(address) {
//Queries House and Senate layers for hits within 1 meter of queried address
let query = new Query();
query.geometry = address;
query.distance = 1;
query.units = "meters";
query.outFields = ["*"];

try{
return query;
} catch(error) {
console.log(error);
}
}

searchWidget.on("select-result", event => {
let query = getQuery(event.result.feature.geometry);

//Only one result per address.
//Senate results. These results are only available in a promise. See Dojo docs.
let senateResults = senateLyr.queryFeatures(query).then(results => {
return results;
}).otherwise(function(error) {
console.log("Senate error message: ", error);
});

//House Results. These results are only available in a promise. See Dojo docs.
let houseResults = houseLyr.queryFeatures(query).then(results => {
return results;
}).otherwise(function(error) {
console.log("House error message: ", error);
});

//Only on search results
if (event.result.sourceIndex == 0) {
all([senateResults, houseResults]).then(results => {

let senateFeatures = results[0].features;
let houseFeatures = results[1].features;
let senateAttributes = results[0].features[0].attributes;
let houseAttributes = results[1].features[0].attributes;

//Combined results
view.popup.open({
title: "Your Legislator Information",
content: '<b>Senator ' + senateAttributes.FIRST_NAME + ' ' + senateAttributes.LAST_NAME + '</b>' +
'<div class="findLegContainer"><div><img class="headshot" src="https://leg.colorado.gov/sites/default/files/' + senateAttributes.MEMBER_PICTURE +'" /></div>' +
'<div><b>'+ senateAttributes.CHAMBER + 'enate District ' + senateAttributes.DISTRICT + '</b><br />' +
'E-mail: <a href="mailto: ' + senateAttributes.EMAIL +'">' + senateAttributes.EMAIL + '</a><br />' +
'Webpage: <a href="https://leg.colorado.gov/node/' + senateAttributes.WEBSITE + '" target="_blank">' + senateAttributes.FIRST_NAME + ' ' + senateAttributes.LAST_NAME + '</a></div></div><b>Representative ' + houseAttributes.FIRST_NAME + ' ' + houseAttributes.LAST_NAME + '</b>' +
'<div class="findLegContainer"><div><img class="headshot" src="https://leg.colorado.gov/sites/default/files/' + houseAttributes.MEMBER_PICTURE +'" /></div>' +
'<div><b>'+ houseAttributes.CHAMBER + 'ouse District ' + houseAttributes.DISTRICT + '</b><br />' +
'E-mail: <a href="mailto: ' + houseAttributes.EMAIL +'">' + houseAttributes.EMAIL + '</a><br />' +
'Webpage: <a href="https://leg.colorado.gov/node/' + houseAttributes.WEBSITE + '" target="_blank">' + houseAttributes.FIRST_NAME + ' ' + houseAttributes.LAST_NAME + '</a></div></div>',
location: event.result.feature.geometry,
});

}).otherwise(error => {
console.log(error);
});

};
});

 

Any help on figuring out why the popup is disappearing after switching back to locator search would be awesome.

Outcomes