Ordering index of markers displayed from search results

764
9
Jump to solution
01-26-2022 10:54 AM
Fox71
by
New Contributor

Hello!

Using the search, if I get a result that is in the same position as a previous result, the icon of the new result is stacked below the icon of the first results and is not visible. Is there a way to make sure the latest result stacks on the top of previous results?

I also have markers that are displayed with toggle buttons. When some are already toggled on the map, the markers from a search result stack up below the toggled makers. Is there a way to make sure search results always stack on the top of any other markers on the map?

 

Thank you!

Ben

0 Kudos
1 Solution

Accepted Solutions
UndralBatsukh
Esri Regular Contributor

Hi there, 

So I looked your test app. I'd suggest adding your search results to a different layer and order the features in that layer by their ObjectIds in descending order to ensure that the latest feature is always displayed over other features. You can order the features by setting orderBy property on the layer. 

I created this simple codepen app to show how this would work: https://codepen.io/U_B_U/pen/XWzbJbq

In this app, I am creating a client-side FeatureLayer as shown below. Notice that I am setting the orderBy parameter to order features by their ObjectIds in descending order.

let searchLayer;
view.whenLayerView(jsonLayer).then(() =>{
  searchLayer = new FeatureLayer({
    source: [], 
    copyright: "USU",
    popupTemplate: jsonLayertemplate,
    fields: jsonLayer.fields,
    renderer: jsonLayerRenderer,
    orderBy: {
      field: "OBJECTID",
      order: "descending"
    },
    objectIdField: "OBJECTID",
    geometryType: "point", 
  });
  map.add(searchLayer);
});

 

Then when the search is completed, you add the search result to this layer as shown below.

searchWidget.on('select-result',function(event){
  layer = event.result.feature.layer;
  const adds = {
    addFeatures: [event.result.feature]
  };
  searchLayer.applyEdits(adds);
});
          

 

I noticed that UniqueValueRenderer you are assigning to your GeoJSONLayer is incomplete. You need to make sure all unique values are included in the renderer. Otherwise, orderBy won't work properly. For example, I had to add general type in the renderer like the following: 

{
  value: "general",
  symbol: {
    type: "picture-marker",
    url: 'https://webdev.usu.edu/map2/images/icons/interest.png',
    width: '32px',
    height: '37px'
  }
},
{
  value: "general building",
  symbol: {
    type: "picture-marker",
    url: 'https://webdev.usu.edu/map2/images/icons/interest.png',
    width: '32px',
    height: '37px'
  }
},

 

Hope this helps,

View solution in original post

0 Kudos
9 Replies
Noah-Sager
Esri Regular Contributor

Hi @Fox71, do you have a simplified app that reproduces this issue? I cannot figure out how to get overlapping icons from a Search widget results with the out of the box widget: 

https://developers.arcgis.com/javascript/latest/sample-code/sandbox/?sample=widgets-search-3d

 

0 Kudos
Fox71
by
New Contributor

Noah,

Sorry for the delay, I had to move my test on a new page. You can look at https://www.usu.edu/test/map/

For the test, search for "huntsman hall" (building). Then search for "school of accountancy". That marker has the same coordinates and shows up behind the other one.

0 Kudos
UndralBatsukh
Esri Regular Contributor

Hi there, 

So I looked your test app. I'd suggest adding your search results to a different layer and order the features in that layer by their ObjectIds in descending order to ensure that the latest feature is always displayed over other features. You can order the features by setting orderBy property on the layer. 

I created this simple codepen app to show how this would work: https://codepen.io/U_B_U/pen/XWzbJbq

In this app, I am creating a client-side FeatureLayer as shown below. Notice that I am setting the orderBy parameter to order features by their ObjectIds in descending order.

let searchLayer;
view.whenLayerView(jsonLayer).then(() =>{
  searchLayer = new FeatureLayer({
    source: [], 
    copyright: "USU",
    popupTemplate: jsonLayertemplate,
    fields: jsonLayer.fields,
    renderer: jsonLayerRenderer,
    orderBy: {
      field: "OBJECTID",
      order: "descending"
    },
    objectIdField: "OBJECTID",
    geometryType: "point", 
  });
  map.add(searchLayer);
});

 

Then when the search is completed, you add the search result to this layer as shown below.

searchWidget.on('select-result',function(event){
  layer = event.result.feature.layer;
  const adds = {
    addFeatures: [event.result.feature]
  };
  searchLayer.applyEdits(adds);
});
          

 

I noticed that UniqueValueRenderer you are assigning to your GeoJSONLayer is incomplete. You need to make sure all unique values are included in the renderer. Otherwise, orderBy won't work properly. For example, I had to add general type in the renderer like the following: 

{
  value: "general",
  symbol: {
    type: "picture-marker",
    url: 'https://webdev.usu.edu/map2/images/icons/interest.png',
    width: '32px',
    height: '37px'
  }
},
{
  value: "general building",
  symbol: {
    type: "picture-marker",
    url: 'https://webdev.usu.edu/map2/images/icons/interest.png',
    width: '32px',
    height: '37px'
  }
},

 

Hope this helps,

0 Kudos
Fox71
by
New Contributor

Hi Undral,

Thank you! I will try that and report back.

Ben

0 Kudos
Fox71
by
New Contributor

Undral,

Fist, many thanks to you for taking the time to build that example, really! I am very new to GIS and not an expert in JS, so there is some struggle....

I was able to get it to work. Following your example, I tried to apply the same concept to a bunch of toggle filters. They also don't stack up correctly in the order they are triggered.
Something I noticed too is that if I click a marker displayed from a search results and if that marker is over a maker triggered from the toggle tools, the opening popup is from that triggered marker. I wonder if the layers needs reorder based on the last user action (search or toggle button).

I rebuilt an example with your update and a couple toggle buttons at https://codepen.io/Fox71/pen/podJEVL?editors=1000. A good test is to trigger the restroom toggle and search for "HH" in the search.

I actually tried to use the searchLayer for the toggle buttons by adding an expression. While it can take an expression, I don't think the geometryType (point) works since the toggle buttons can return many points. Just my assumption. Let me know what you think if you can.

Thanks again!

Ben

0 Kudos
UndralBatsukh
Esri Regular Contributor

Hi there, 

The popup order is decided by response time of layers. This is the behavior at this time. We have an enhancement to show popup results in the render order. I will update you once this is installed. I don't have any info when this will be installed. 

 

0 Kudos
Fox71
by
New Contributor

Thank you!

0 Kudos
Fox71
by
New Contributor

I noticed a warning with the code in the console log: "feature-layer:spatial-reference-not-found"

I assume this is regarding that search layer. Is there anything I need to do to suppress this warning?

As a reference, the sample code is still posted at https://codepen.io/U_B_U/pen/XWzbJbq?editors=1010

Thank you,

Ben

0 Kudos
UndralBatsukh
Esri Regular Contributor

I updated the codepen. You need to set the spatialReference of the FeatureLayer when initializing it. 

0 Kudos