给定一组位置,如何确定Bing地图的相应缩放级别和地图中心?

时间:2014-11-08 13:18:00

标签: c# geolocation bing-maps

我使用Bing地图在Windows应用商店应用(C#)上工作。

我希望能够,在给定地点(纬度和经度对)的集合的情况下,确定地图的缩放级别应该是什么,以及它的中心点(位置)应该是什么。

从位置值的集合中,我提取了四个"极端"需要显示的基本点(最北,南,东,西)。

IOW,如果我想在整个旧金山展示图钉,我想让缩放级别显示该城市,仅此而已。如果我想显示散布在美国各地的图钉,那么你就可以了解情况。

这是我到目前为止所做的(只是粗略的草稿/伪代码,如你所见):

确定一组位置的极端基数值(代码未显示;应该是微不足道的)。创建我的自定义类的实例:

public class GeoSpatialBoundaries
{
    public double furthestNorth { get; set; }
    public double furthestSouth { get; set; }
    public double furthestWest { get; set; }
    public double furthestEast { get; set; }
}

...然后调用这些方法,传递该实例:

// This seems easy enough, but perhaps my solution is over-simplistic
public static Location GetMapCenter(GeoSpatialBoundaries gsb)
{
    double lat = (gsb.furthestNorth + gsb.furthestSouth) / 2;
    double lon = (gsb.furthestWest + gsb.furthestEast) / 2;
    return new Location(lat, lon);
}

// This math may be off; just showing my general approach
public static int GetZoomLevel(GeoSpatialBoundaries gsb)
{
    double latitudeRange = gsb.furthestNorth - gsb.furthestSouth;
    double longitudeRange = gsb.furthestEast - gsb.furthestWest;
    int latZoom = GetZoomForLat(latitudeRange);
    int longZoom = GetZoomForLong(longitudeRange);
    return Math.Max(latZoom, longZoom);
}

在这里,我真的迷路了。如何根据这些值确定要返回的缩放级别(介于1..20之间)?这是一个非常粗略的想法(GetZoomForLat()基本相同):

// Bing Zoom levels range from 1 (the whole earth) to 20 (the tippy-top of the cat's whiskers)
private static int GetZoomForLong(double longitudeRange)
{
    // TODO: What Zoom level ranges should I set up as the cutoff points? IOW, should it be something like:
    if (longitudeRange > 340) return 1;
    else if (longitudeRange > 300) return 2;
    // etc.? What should the cutoff points be?
    else return 1;
}

有没有人有任何建议或链接可以指出我如何实现这些功能?

2 个答案:

答案 0 :(得分:1)

您可以使用LocationRect类设置边界框,请参阅MSDN:

http://msdn.microsoft.com/en-us/library/hh846491.aspx

然后您使用Map类及其SetView()方法,请参阅MSDN:

http://msdn.microsoft.com/en-us/library/hh846504.aspx

这是一个可行的代码(map是你的地图控件实例):

var collection = new LocationCollection();
collection.Add(new Location(47.5, 2.75));
collection.Add(new Location(48.5, 2.75));
collection.Add(new Location(43.5, 5.75));
map.SetView(new LocationRect(collection));

因此,您可以使用要在集合中的地图上显示的元素的每个坐标来生成边界框并相应地设置视图。

答案 1 :(得分:1)

我在不久前写了一篇关于如何做到这一点的博文:http://rbrundritt.wordpress.com/2009/07/21/determining-best-map-view-for-an-array-of-locations/

相关问题