Select to view content in your preferred language

MapView.SetViewpoint changes the passed parameters' YMin and YMax

690
2
Jump to solution
04-29-2024 11:28 PM
RenzSui
Emerging Contributor

Hello,

I'm currently working on integrating arcgis runtime in our software and I have encountered an impediment wherein whenever I set the viewpoint, it seems after calling UpdateLayout, the Y values set in MapView.VisibleArea is entirely different.

Here's my code:

 

var tl = new MapPoint(x1, y1, CurrentSpatialReference);
var br = new MapPoint(x2, y2, CurrentSpatialReference);            
if(CurrentSpatialReference != null)
{
    var projTl = (MapPoint)tl.Project(SpatialReferences.WebMercator);
    var projBr = (MapPoint)br.Project(SpatialReferences.WebMercator);
        Trace.WriteLine("Mercator coord hand calc: " + projTl.ToString() + 
        " " + projBr.ToString());

    var backTl = (MapPoint)projTl.Project(CurrentSpatialReference);
    var backBr = (MapPoint)projBr.Project(CurrentSpatialReference);
        Trace.WriteLine("Source coord hand calc: " + backTl.ToString() + 
        " " + backBr.ToString());

    BoundingBox = new Envelope(projTl, projBr);                
}
var vp = new Viewpoint(BoundingBox);
MapView.SetViewpoint(vp);
MapView.UpdateLayout();

var geom = MapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry);

 

The initialization of MapView happens during the construction of my WPF window.

 

int width = 1200, height = 700;
MyMapView.SetDimension(width, height);
MyMapView.DrawStatusChanged += MyMapView_DrawStatusChanged;
MyMapView.GeoViewTapped += MyMapView_GeoViewTapped;
MyMapView.Map = new Map(BasemapStyle.OSMStandard);

 

 

Here's a sample run that I did. This is using British national grid as the spatial reference from the source. The spatial reference used by MyMapView is SpatialReferences.WebMercator. Current Viewpoint's value is fetched using the GetCurrentViewpoint(Viewpoint.Type.BoundingGeometry).

Thank you!

 

 

ExecuteCommand: Update|643332.964833|262073.813165|645936.153221|263557.442529
Mercator coord hand calc: MapPoint[X=173625.12544513, Y=6836938.46023434, Wkid=3857] MapPoint[X=177976.921476025, Y=6839147.08684191, Wkid=3857]
Source coord hand calc: MapPoint[X=643332.965807652, Y=262073.812804036, Wkid=27700] MapPoint[X=645936.154196948, Y=263557.442169608, Wkid=27700]
Current Viewpoint: MapPoint[X=173625.12544513, Y=6836857.60963878, Wkid=3857] MapPoint[X=177976.921476025, Y=6839227.93743747, Wkid=3857]
Projected Viewpoint: MapPoint[X=643335.398222287, Y=262024.325764331, Wkid=27700] MapPoint[X=645933.695298256, Y=263606.914711113, Wkid=27700]

 

 

 

0 Kudos
1 Solution

Accepted Solutions
dotMorten_esri
Esri Notable Contributor

I'm a little bit confused about the sample and what you're expecting to happen. For instance I don't see a "SetDimension" method on the view.

Second I'm not quite sure why you're calling UpdateLayout - this just forces a XAML layout cycle, but you really shouldn't need to call this. Getting the viewpoint immediately after SetViewpoint likely won't work either, since the rendering thread is separate and will update slightly after - I'd suggest using the ViewpointChanged event to listen for changes to the viewpoint. You could also try using the async version and await the set-viewpoint operation:
    await SetViewpointAsync(vp);
    var geom = MapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry);

One more thing to consider: When calling SetViewpoint with a bounding geometry, we'll do the best to fix that geometry into the view. If your mapview is a wide landscape view (black box), and the bounding geometry is portrait shaped (red box), we'll fit the geometry to the view around the top and bottom: 

dotMorten_esri_0-1714493104038.png

Similarly if the box is very wide, we'll fit it the other way, again guaranteeing that the geometry you're trying to zoom to is fully visible:

dotMorten_esri_1-1714493225626.png

That means the box you're passing in won't be the box you get back out, since we have to adjust for the different aspect ratios.

View solution in original post

2 Replies
dotMorten_esri
Esri Notable Contributor

I'm a little bit confused about the sample and what you're expecting to happen. For instance I don't see a "SetDimension" method on the view.

Second I'm not quite sure why you're calling UpdateLayout - this just forces a XAML layout cycle, but you really shouldn't need to call this. Getting the viewpoint immediately after SetViewpoint likely won't work either, since the rendering thread is separate and will update slightly after - I'd suggest using the ViewpointChanged event to listen for changes to the viewpoint. You could also try using the async version and await the set-viewpoint operation:
    await SetViewpointAsync(vp);
    var geom = MapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry);

One more thing to consider: When calling SetViewpoint with a bounding geometry, we'll do the best to fix that geometry into the view. If your mapview is a wide landscape view (black box), and the bounding geometry is portrait shaped (red box), we'll fit the geometry to the view around the top and bottom: 

dotMorten_esri_0-1714493104038.png

Similarly if the box is very wide, we'll fit it the other way, again guaranteeing that the geometry you're trying to zoom to is fully visible:

dotMorten_esri_1-1714493225626.png

That means the box you're passing in won't be the box you get back out, since we have to adjust for the different aspect ratios.

RenzSui
Emerging Contributor

Thank you for the reply. The explanation in the aspect ratio is clear. The imprecision is caused likely by the aspect ratio of the client window's dimension that I passed differing from the aspect ratio passed into set viewpoint. .

0 Kudos