Get view bounds of a Map

后端 未结 2 1424
無奈伤痛
無奈伤痛 2020-12-10 21:59

I\'m developing a Windows Phone 8.1 app that works with Bing Maps. During the rendering of this map I use the TrySetViewBoundsAsync to set correctly my custom

2条回答
  •  臣服心动
    2020-12-10 22:29

    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:

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

提交回复
热议问题