【问题标题】:Given a collection of Locations, how can I determine the appropriate Zoom level and Map Center for Bing Maps?给定一组位置,我如何确定 Bing 地图的适当缩放级别和地图中心?
【发布时间】:2015-01-05 04:58:33
【问题描述】:

我正在使用 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);
}

不过,这才是我真正迷路的地方。如何根据这些 val 确定要返回的缩放级别(在 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;
}

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

【问题讨论】:

    标签: c# geolocation bing-maps


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      可以使用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));
      

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

      【讨论】:

      • 完美,尼古拉斯!我会尽快奖励你的答案。
      猜你喜欢
      • 2021-05-29
      • 2021-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-25
      • 2017-01-14
      • 2013-03-12
      相关资源
      最近更新 更多