Select to view content in your preferred language

remove highlight flashes on hover

669
2
Jump to solution
12-20-2022 06:11 AM
LeidavanHees
Occasional Contributor

Hello!

 

I'd like to know how to check if a feature is already highlighted.

I added a highlight for features on mousehover. But the highlight keeps flashing off and on if you move the mouse within a feature.

I think this is because I remove the highlight on every mouse move, without checking if a feature is already highlighted first. I've tried to build in a check for this, but it doesn't work yet. Which is why I'd like to know how to check for this.

 

Any help would be much appreciated!

 

 

Code (V4.24):

		view.when(() => {
			let highlight = null;

			view.on("pointer-move", evt => {
				view.hitTest(evt).then(response => {
					if (response.results.length > 0) {

						if (highlight) {
							highlight.remove();
						}

						let graphic = response.results[0].graphic;
						view.whenLayerView(graphic.layer).then(function(layerView){
							highlight = layerView.highlight(graphic);
						});

					}
					else{
						if (highlight) {
							highlight.remove();
						}

					}
				});
			});
		});

 

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
UndralBatsukh
Esri Regular Contributor

Hi there, 

This sample https://developers.arcgis.com/javascript/latest/sample-code/sandbox/?sample=view-hittest shows you how to do this. Namely take a look at the getGraphics function where we are checking if the some attributes of the graphic returned from hottest matches the attributes of the already highlighted graphic like so: 

const graphic = response.results[0].graphic;

const attributes = graphic.attributes;
const category = attributes.CAT;
const wind = attributes.WIND_KTS;
const name = attributes.NAME;
const year = attributes.YEAR;
const id = attributes.OBJECTID;

if (highlight && (currentName !== name || currentYear !== year)) {
  highlight.remove();
  highlight = null;
  return;
}

if (highlight) {
  return;
}

 

View solution in original post

0 Kudos
2 Replies
UndralBatsukh
Esri Regular Contributor

Hi there, 

This sample https://developers.arcgis.com/javascript/latest/sample-code/sandbox/?sample=view-hittest shows you how to do this. Namely take a look at the getGraphics function where we are checking if the some attributes of the graphic returned from hottest matches the attributes of the already highlighted graphic like so: 

const graphic = response.results[0].graphic;

const attributes = graphic.attributes;
const category = attributes.CAT;
const wind = attributes.WIND_KTS;
const name = attributes.NAME;
const year = attributes.YEAR;
const id = attributes.OBJECTID;

if (highlight && (currentName !== name || currentYear !== year)) {
  highlight.remove();
  highlight = null;
  return;
}

if (highlight) {
  return;
}

 

0 Kudos
LeidavanHees
Occasional Contributor

Thank you! I thought I couldn't use this because I have different layers with different attributes. But I looked at it again and am now using the OBJECTID, which works like a charm.

0 Kudos