Get distance of MapControl border in degrees

2019-08-09 15:47发布

问题:

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

First example:

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:

~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...

回答1:

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;


回答2:

Got it:

Longitude:

360 * Math.Pow(0.5, mcMapControl.ZoomLevel - 1) * mcMapControl.ActualWidth / 409.5;

Latitude:

180 * Math.Pow(0.5, mcMapControl.ZoomLevel - 1) * mcMapControl.ActualHeight / 409.5;