简体   繁体   English

获取地图的视图边界

[英]Get view bounds of a Map

I'm developing a Windows Phone 8.1 app that works with Bing Maps. 我正在开发一个适用于Bing Maps的Windows Phone 8.1应用程序。 During the rendering of this map I use the TrySetViewBoundsAsync to set correctly my custom view. 在渲染此映射期间,我使用TrySetViewBoundsAsync来正确设置我的自定义视图。 But now I want to get this information (after the user changes the view by zooming/moving the map) but I don't find any method that helps me. 但现在我想获取这些信息(在用户通过缩放/移动地图来更改视图之后),但我找不到任何方法可以帮助我。

How can I get the view bounds? 我怎样才能获得视图边界?

There isn't a built in method for this, however it can be done fairly easily. 没有内置的方法,但它可以很容易地完成。 Here is a bit of code for this which I pulled from the Microsoft Maps Spatial Toolbox project : 以下是我从Microsoft Maps Spatial Toolbox项目中提取的一些代码:

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;
}

Note that rbrundritt's solution will not work for oblique (tilted) views. 请注意, rbrundritt的解决方案不适用于倾斜(倾斜)视图。 In that case, the visible region resembles more an inverted trapezoid than a bounding box. 在这种情况下,可见区域类似于倒角梯形而不是边界框。 Also the top-left corner might not be a valid location if the horizon is visible. 如果地平线可见,左上角可能也不是有效位置。

For Windows 10 Anniversary Update (version 1607), the MapControl supports a new method GetVisibleRegion() to help you with this. 对于Windows 10周年更新(版本1607), MapControl支持新方法GetVisibleRegion()来帮助您。

The following should return you the view bounds of the map: 以下应该返回地图的视图边界:

map.GetVisibleRegion(MapVisibleRegionKind.Full)

See the MapControl documentation for more details. 有关更多详细信息,请参阅MapControl文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM