Windows Phone 8.1获取地图角落geopoint

时间:2015-07-27 20:30:20

标签: c# algorithm windows-phone-8.1 bing-maps geopoints

我试图找到角落的地理位置

  • 左上
  • TopRight
  • BOTTOMLEFT
  • BottomRight

MapControl提供的信息是

  • 中心(Geopoint)
  • ZoomLevel(Double min:1,max:20)
  • ActualHeight(Double)
  • ActualWidth(Double)

根据这些信息,我可以找到角落吗?

我在想这样的事情:

double HalfHeight = Map.ActualHeight / 2;
double HalfWidth = Map.ActualWidth / 2;

这意味着Center Geopoint位于HalfWdidth(X)和HalfHeight(Y)。能以某种方式帮助我吗?

修改:我的问题与提到thisrbrundritt问题非常相似,但它只提供了TopLeft和BottomRight。基于该问题的接受答案(由rbrundritt提供),我还完成了另外两个并将它们包装在Extension中,请查看下面的答案。谢谢rbrundritt。

1 个答案:

答案 0 :(得分:0)

public static class MapExtensions
{
    private static Geopoint GetCorner(this MapControl Map, double x, double y, bool top)
    {
        Geopoint corner = null;

        try
        {
            Map.GetLocationFromOffset(new Point(x, y), out corner);
        }
        catch
        {
            Geopoint position = new Geopoint(new BasicGeoposition()
            {
                Latitude = top ? 85 : -85,
                Longitude = 0
            });

            Point point;
            Map.GetOffsetFromLocation(position, out point);
            Map.GetLocationFromOffset(new Point(0, point.Y), out corner);
        }

        return corner;
    }

    public static Geopoint GetTopLeftCorner(this MapControl Map)
    {
        return Map.GetCorner(0, 0, true);
    }

    public static Geopoint GetBottomLeftCorner(this MapControl Map)
    {
        return Map.GetCorner(0, Map.ActualHeight, false);
    }

    public static Geopoint GetTopRightCorner(this MapControl Map)
    {
        return Map.GetCorner(Map.ActualWidth, 0, true);
    }

    public static Geopoint GetBottomRightCorner(this MapControl Map)
    {
        return Map.GetCorner(Map.ActualWidth, Map.ActualHeight, false);
    }
}
相关问题