Tobias H Tobias H - 4 months ago 33
C# Question

Get distance of MapControl border in degrees

I have a MapControl and would like to know how many degrees are currently shown on the x and y axes.

First example:
map landscape

360 degrees are shown on the x axis (longitude)

~90 degrees are shown on the y axis (latitude)

(The zoom level is 3.2 and it's max zoomed out)

Second example:
map portrait

~220 degrees on the x axis (longitude)

180 degrees on the y axis (latitude)

(zoom level: 1.7; max zoomed out)

I tried calculating the current degrees on the x axis using following code:

double dist = 360 * Math.Pow(0.5, macSurrounding.ZoomLevel - 1);

but it doesn't work, because the zoom level is just strange...


You should use the MapControl's GetLocationFromOffset method to calculate the geographic coordinates of the south-east and north-west corner points of the current map viewport. The width and height of the viewport would be the latitude and longitude differences of these points.

Geopoint northEast;
Geopoint southWest;

map.GetLocationFromOffset(new Point(map.ActualWidth, 0), out northEast);
map.GetLocationFromOffset(new Point(0, map.ActualHeight), out southWest);

var width = northEast.Position.Longitude - southWest.Position.Longitude;
var height = northEast.Position.Latitude - southWest.Position.Latitude;