Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

c# - Get view bounds of a Map

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 view. 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...