如何将Bing Map的缩放级别设置为足以显示所有图钉的宽度? [英] How to set the zoom level of Bing Map to just wide enough to display all pushpins?

查看:107
本文介绍了如何将Bing Map的缩放级别设置为足以显示所有图钉的宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我向Bing地图添加一系列 Pushpins 时,我希望缩放级别自动更新为最佳".-最近的"可以显示所有图钉的视图.

我想这可以通过计算所需的缩放级别来实现,方法是找到最北和最南的纬度值,以及最东和西最远的经度值,然后以这些值的平均值为中心

我的想法是将 GeoCoordinates 的列表传递给一个方法,该方法将返回适当的ZoomLevel,并且可能还有一个单独的方法来计算地图的中心点.

然后可以将这两个值用于地图的 SetView(location,ZoomLevel)方法.

这已经完成了吗,或者有Bing Maps呼叫可以处理吗?

解决方案

注意::如果您没有位置列表,但是地图上只有几个图钉,那么您可以轻松地获得如下位置:

  var map = this.userControl11.myMap;var location = map.Children.OfType< Pushpin>().Select(x => x.Location); 

When I add a slew of Pushpins to a Bing Map, I want the zoom level to automagically update to the "best fit" - the "closest in" view that allows all of the Pushpins to be displayed.

I would imagine that it would be done by computing the zoom level required by discovering the latitude values that are the furthest north and southmost, and the longitude values that are furthest east and west, and then centering on the average value of those.

My idea is passing a List of GeoCoordinates to a method that would return the appropriate ZoomLevel, and possibly a separate method that would calculate the center point of the map.

These two values could then be used for the map's SetView(location, ZoomLevel) method.

Has this been done already, or is there a Bing Maps call that will handle this?

解决方案

There is an overload of SetView which accepts a list of locations and set the view to the bounding rectangle of those locations. It ensures all the locations are visible; you also need to add a bit of extra margins to the view rectangle to make sure all the pushpins are visible.

Having pushpins on the map, means you already have a list of locations or if you don't have you can easily get the locations of all those pushpins.

Example

Assuming you have followed the steps on How can I add a Bing Maps Component to my C# Winforms app?, add a button to the form and handle its click event like this:

private void button1_Click(object sender, EventArgs e)
{
    var map = this.userControl11.myMap;

    //Locations
    var locations = new[] {
        new Location(47.6424, -122.3219),
        new Location(47.8424, -122.1747),
        new Location(47.67856, -122.130994)};

    //Add Pushpins
    locations.Select(x => new Pushpin() { Location = x })
        .ToList().ForEach(x => { map.Children.Add(x); });

    //Margin
    var w = new Pushpin().Width;
    var h = new Pushpin().Height;
    var margin = new Thickness(w / 2, h, w / 2, 0);

    //Set view
    map.SetView(locations, margin, 0);
}

And this is the result:

Note: If you don't have the list of locations, but you have just a few pushpins on the map, then you can easily get locations like this:

var map = this.userControl11.myMap;
var locations = map.Children.OfType<Pushpin>().Select(x => x.Location);

这篇关于如何将Bing Map的缩放级别设置为足以显示所有图钉的宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆