【发布时间】:2014-06-28 15:22:11
【问题描述】:
我正在开发一个适用于 Bing 地图的 Windows Phone 8.1 应用。
在此地图的渲染过程中,我使用TrySetViewBoundsAsync 来正确设置我的自定义视图。但是现在我想获取此信息(在用户通过缩放/移动地图更改视图之后),但我没有找到任何对我有帮助的方法。
如何获得视图边界?
【问题讨论】:
标签: c# bing-maps windows-phone-8.1
我正在开发一个适用于 Bing 地图的 Windows Phone 8.1 应用。
在此地图的渲染过程中,我使用TrySetViewBoundsAsync 来正确设置我的自定义视图。但是现在我想获取此信息(在用户通过缩放/移动地图更改视图之后),但我没有找到任何对我有帮助的方法。
如何获得视图边界?
【问题讨论】:
标签: c# bing-maps windows-phone-8.1
没有内置的方法,但是它可以很容易地完成。这是我从Microsoft Maps Spatial Toolbox project 中提取的一些代码:
public static GeoboundingBox GetBounds(this MapControl map)
{
Geopoint topLeft = null;
try
{
map.GetLocationFromOffset(new Windows.Foundation.Point(0, 0), out topLeft);
}
catch
{
var topOfMap = new Geopoint(new BasicGeoposition()
{
Latitude = 85,
Longitude = 0
});
Windows.Foundation.Point topPoint;
map.GetOffsetFromLocation(topOfMap, out topPoint);
map.GetLocationFromOffset(new Windows.Foundation.Point(0, topPoint.Y), out topLeft);
}
Geopoint bottomRight = null;
try
{
map.GetLocationFromOffset(new Windows.Foundation.Point(map.ActualWidth, map.ActualHeight), out bottomRight);
}
catch
{
var bottomOfMap = new Geopoint(new BasicGeoposition()
{
Latitude = -85,
Longitude = 0
});
Windows.Foundation.Point bottomPoint;
map.GetOffsetFromLocation(bottomOfMap, out bottomPoint);
map.GetLocationFromOffset(new Windows.Foundation.Point(0, bottomPoint.Y), out bottomRight);
}
if (topLeft != null && bottomRight != null)
{
return new GeoboundingBox(topLeft.Position, bottomRight.Position);
}
return null;
}
【讨论】:
请注意,rbrundritt's solution 不适用于倾斜(倾斜)视图。在这种情况下,可见区域更像一个倒梯形而不是边界框。此外,如果地平线可见,左上角可能不是有效位置。
对于 Windows 10 周年更新(版本 1607),MapControl 支持一种新方法 GetVisibleRegion() 来帮助您。
以下内容应返回地图的视图边界:
map.GetVisibleRegion(MapVisibleRegionKind.Full)
有关详细信息,请参阅MapControl 文档。
【讨论】: