Select to view content in your preferred language

View goTo target, looking for the best solution if there are many polygons and graphics.

2720
29
12-28-2022 03:31 AM
DimaY
by
New Contributor

At a certain user's choice, I draw the necessary polygons at the given coordinates from the given object and add them to the map in the form of graph figures. Then I want to show the selection on the map and use method GOTO.  Polygons are added in a loop.  I've already tried all sorts of ways. And zoomed in. And changed in a cycle. And after the cycle. And in the end, it's still kind of clumsy. That's not in the right place. Either too small or too large. If, for example, the United States, then it is not in the middle at all. How best to use To zoom the user on the map to the right place? Is there a better optimal solution? Has anyone encountered similar problems?

my code is something like this :

 

const map = new Map({
ground: "world-elevation",
basemap: grayBasemap
});

const view = new SceneView({
container,
map: map,
qualityProfile: "high",
zoom: 4
});

this.view.graphics.removeAll();

for (var i = 0; i < objCoordinates.length; i++) {
var coordinates = objCoordinates[0];
var polygon = new Polygon();
polygon.addRing(coordinates);

var polygonGraphic = new Graphic({
geometry: polygon,
symbol: fillSymbol
});

this.view.graphics.add(polygonGraphic);
}

this.view.goTo({
target: this.view.graphics
}).catch(this.catchAbortError);

0 Kudos
29 Replies
DimaY
by
New Contributor

I was so happy that there was finally a solution to this unsolvable problem. But unfortunately, everything is the same without changes. The problem hasn't been resolved.

0 Kudos
JoelBennett
MVP Regular Contributor

I wasn't certain that would work, but it was worth a try.  In this case, I think you must fix the extent so that it starts on the left-hand side of the international dateline, and extends eastward across it.  You should add this function somewhere in your code:

function fixExtent(extent) {
	var extents = extent.normalize();

	if (extents.length === 1)
		return extents[0];
	else {
		var westExtent, eastExtent;

		if (extents[0].xmax > extents[1].xmax) {
			westExtent = extents[0];
			eastExtent = extents[1];
		} else {
			westExtent = extents[1];
			eastExtent = extents[0];
		}

		westExtent.xmax += eastExtent.width;

		return westExtent;
	}
}

 

Then, you would change the call to goTo to be

this.view.goTo(fixExtent(extent)).catch(this.catchAbortError);
0 Kudos
DimaY
by
New Contributor

Once again I want to thank you for your complicity in the problem and for your answers. Thank you! Here, from the documentation and the code, another question appeared. The documentation recommends, in a case similar to my code, to use extent clone.  Although I do not really understand why and what it changes and gives in the end. But the question is this from the code. 

if (extent === null)
extent = polygon.extent.clone();
else
extent.union(polygon.extent);

Why don't  use clone everywhere?  For example extent.union(polygon.extent);?? 

extent.union(polygon.extent.clone());

Could this have something to do with it and make some difference?

0 Kudos
JoelBennett
MVP Regular Contributor

We should clone the extent because we plan to modify the values of that extent object, and we eliminate the potential of those changes causing problems for the owning polygon object.  (In this particular case, it probably wouldn't matter, but it's just good practice.)

In the line:

extent.union(polygon.extent);

only "extent" is modified, not "polygon.extent", so there is no need to clone "polygon.extent".

0 Kudos
DimaY
by
New Contributor
if (extent === null)
		extent = polygon.extent.clone();
	else
		extent.union(polygon.extent);

Interesting. Then how do you explain this line? extent = polygon.extent.clone();

This is not a polygon extent clone?

 

0 Kudos
JoelBennett
MVP Regular Contributor

As stated previously, "We should clone the extent because we plan to modify the values of that extent object".  In the first pass of the loop, we clone the first polygon's extent, because in each subsequent pass, that object will be modified in place by the union function.

0 Kudos
DimaY
by
New Contributor

Unfortunately, this did not help in solving the problem. Everything is exactly the same as it was before.

0 Kudos
DimaY
by
New Contributor

I will also add that I get the length  if (extents.length === 1) in any case all the time equal to 1. 

0 Kudos
JoelBennett
MVP Regular Contributor

Ok, this makes it somewhat difficult, because the full width of the calculated extent is the full width of the coordinate system itself.  The map therefore centers at the Prime Meridian.

Does your dataset include data throughout the world, or just the US and the Pacific?

0 Kudos
DimaY
by
New Contributor

In various situations and cases, I receive information about an object that comes to me in the form of objects, one of the objects is GeoJson, which has coordinates. All cases from any other regions of the world work quite well with zoom. And only this case, as I showed in the pictures when I wrote the topic. For some reason it does not rotate the globe and stops and does not zoom. I don't understand why at all.

0 Kudos