I've got a question about zooming and centering using Mapsui on a WinUI project.
Why does the map reset its centering when I perform ZoomToLevel? This is my code:
public MainWindow()
{
InitializeComponent();
MyMapControl.Map.Layers.Add(OpenStreetMap.CreateTileLayer());
Debug.WriteLine($"There are {MyMapControl.Map.Layers[0].Resolutions.Count} zoom levels available");
// Center map on 10.33333, 44.4444444
CenterMapOnPosition(MyMapControl.Map, 10.33333, 44.4444444);
ZoomMapToLevel(MyMapControl.Map, 5);
}
private void CenterMapOnPosition(Map map, double lon, double lat)
{
// Get coordinates in spherical mercator
MPoint sphericalMercatorPoint = SphericalMercator.FromLonLat(lon, lat).ToMPoint();
// Center map on coordinates
map.Home = n => n.CenterOn(sphericalMercatorPoint);
}
private void ZoomMapToLevel(Map map, int level)
{
// Zoom map at specified level
map.Home = n => n.ZoomToLevel(level);
}
When I call CenterMapOnPosition() the map is centered correctly. But when I call ZoomMapToLevel(), then map centering goes back to somewhere near africa.
I know I can do
MyMap.Map.Home = n => n.CenterOnAndZoomTo(mercator, MyMap.Map.Layers[0].Resolutions[18]);
but I need to have the two actions separated.
What am I doing wrong?
I'm expecting the map to remain centered on the specified position when I call ZoomMapToLevel(). I tried to use n.ZoomTo() instead of n.ZoomToLevel() but same problem. Seems like zooming resets the map position at it's standard center, but with correct zoom.