Select to view content in your preferred language

Checking if a point is within a rotated map

152
2
Jump to solution
10-09-2024 11:17 AM
johnms93
New Contributor

Hi there. I have code which checks if a point is within the visible area of the map. This works fine if the map is not rotated. I'm wondering if this is intended, or if additional handling of rotation is required.

This is roughly what I'm doing:
```
MapView mapView;
MapObject myMapObject;
...
public Envelope getVisibleArea() {
  Polygon visibleArea = mapView.getVisibleArea();
  return (Envelope) GeometryEngine.project(visibleArea.getExtent(), SpatialReferences.getWgs84());
}

public boolean isMyMapObjectInVisibleArea() {
  Envelope visibleAreaEnvelope = getVisibleArea();
  Envelope myMapObjectEnvelope= myMapObject.getGeoElement().getGeometry().getExtent();
  return GeometryEngine.within(myMapObjectEnvelope, visibleAreaEnvelope);
}
```

The map object geometry / coordinates are in WGS84, which is why I project the visible area like that. As long as I don't rotate the map, this works as expected when I pan in-and-out. Once I rotate the map, the map object can be outside of the visible area, yet returns true. I need to go much further out for it to finally return false. Why is that? Does the visible area not account for rotation?

Thanks for any help.
   

0 Kudos
1 Solution

Accepted Solutions
Nicholas-Furness
Esri Regular Contributor

I'm guessing the issue is that you're getting envelopes and working with them. Compare the visibleArea polygon (rather than its envelope) against the geoElement's geometry (rather than its envelope).

Envelopes are always orthogonal to the spatial reference's coordinate space, so in the case of the viewArea, a polygon that's rotated 45º will result in an envelope encompassing a lot of space outside that polygon.

And in the case of the geoElement's point geometry, the envelope will have zero width and height, so it's just adding work to the comparison. Much better just to use the point.

Let me know if that helps.

View solution in original post

0 Kudos
2 Replies
Nicholas-Furness
Esri Regular Contributor

I'm guessing the issue is that you're getting envelopes and working with them. Compare the visibleArea polygon (rather than its envelope) against the geoElement's geometry (rather than its envelope).

Envelopes are always orthogonal to the spatial reference's coordinate space, so in the case of the viewArea, a polygon that's rotated 45º will result in an envelope encompassing a lot of space outside that polygon.

And in the case of the geoElement's point geometry, the envelope will have zero width and height, so it's just adding work to the comparison. Much better just to use the point.

Let me know if that helps.

0 Kudos
johnms93
New Contributor

That was indeed the issue! When I was dealing with a visible area I assumed I'd need to convert it to an envelope to check within, especially when I saw an "extent" call, but I see that is completely false and actually problematic in my case. I have switched to using the polygon and point geometries for this check which is working well. You have cleared a lot up for me.

Thanks a lot for your help.

0 Kudos