如何使用 LocationCollection 缩放以适应 WP7 Bing Maps 控件?

发布于 2024-10-11 22:34:37 字数 527 浏览 9 评论 0原文

如何在 Windows Phone 7 上将 Microsoft.Phone.Controls.Maps.Map 控件缩放到正确的缩放级别?

我有一个地理坐标的 LocationCollection,并且我自己计算了中心,但现在如何计算正确的缩放级别以适合 LocationCollection?

PS 有没有一种开箱即用的方法来计算地理坐标的中心,这样我就不必自己计算它?

编辑: 我找到了另一个很好的解决方案: http://4mkmobile.com/2010/09/quick-tip-position-a-map-based-on-a-collection-of-pushpins/

map.SetView(LocationRect .CreateLocationRect(points));

How can I zoom the Microsoft.Phone.Controls.Maps.Map control to the correct zoom level on Windows Phone 7?

I have a LocationCollection of GeoCoordinates and I calculated the Center myself, but now how do I calculate the correct zoom level to fit the LocationCollection?

P.S. Is there an out of the box method to calculate the center of GeoCoordinates so I don't have to calculate it myself?

EDIT:
I've found another fine solution: http://4mkmobile.com/2010/09/quick-tip-position-a-map-based-on-a-collection-of-pushpins/

map.SetView(LocationRect.CreateLocationRect(points));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

从﹋此江山别 2024-10-18 22:34:37

您可以使用以下代码计算包围一组点的 LocationRect,然后将 LocationRect 传递给 SetView() 方法地图控件:

var bounds = new LocationRect(
    points.Max((p) => p.Latitude),
    points.Min((p) => p.Longitude),
    points.Min((p) => p.Latitude),
    points.Max((p) => p.Longitude));
map.SetView(bounds);

地图控件处理从当前位置到新位置的动画。

注意:您需要使用 System.Linqusing 语句来获取 MinMax 方法。

You can use the following code to calculate the LocationRect that bounds a set of points, and then pass the LocationRect to the SetView() method on the map control:

var bounds = new LocationRect(
    points.Max((p) => p.Latitude),
    points.Min((p) => p.Longitude),
    points.Min((p) => p.Latitude),
    points.Max((p) => p.Longitude));
map.SetView(bounds);

The map control handles animating from the current position to the new location.

NOTE: You'll need a using statement for System.Linq to get the Min and Max methods.

丿*梦醉红颜 2024-10-18 22:34:37

德里克已经给出了答案,所以你应该接受他的答案,我为有很多点的情况提供了替代代码。这种方法仅迭代点集合一次而不是 4 次,但它在美观上并不令人愉悦。

 double north, west, south, west;

 north = south = points[0].Latitude;
 west = east = points[0].Longitude;

 foreach (var p in points.Skip(1))
 {
     if (north < p.Latitude) north = p.Latitude;
     if (west > p.Longitude) west = p.Longitude;
     if (south > p.Latitude) south = p.Latitude;
     if (east < p.Longitude) east = p.Longitude
 }
 map.SetView(new LocationRect(north, west, south, east));

Derek has already given the answer so you should accept his, I offer an alternative code for cases where there many points. This approach only iterates the points collection once instead 4 times however it isn't as asthetically pleasing.

 double north, west, south, west;

 north = south = points[0].Latitude;
 west = east = points[0].Longitude;

 foreach (var p in points.Skip(1))
 {
     if (north < p.Latitude) north = p.Latitude;
     if (west > p.Longitude) west = p.Longitude;
     if (south > p.Latitude) south = p.Latitude;
     if (east < p.Longitude) east = p.Longitude
 }
 map.SetView(new LocationRect(north, west, south, east));
转角预定愛 2024-10-18 22:34:37

正如其他答案所建议的,我将 SetViewLocationRect 一起使用。

然而我发现它总是产生低缩放级别,因为只使用整数值。例如,如果完美的缩放级别是 5.5,那么您将得到 5.0。为了获得合适的尺寸,我根据 TargetZoomLevelTargetBoundingRectangle 计算新的缩放级别:

viewRect = LocationRect.CreateLocationRect(coordinates);
map.SetView(viewRect);
double scale = map.TargetBoundingRectangle.Height/viewRect.Height;
map.ZoomLevel = map.TargetZoomLevel + Math.Log(scale, 2);

此示例将缩放级别设置为适合 viewRect 的高度屏幕。

As suggested by the other answers I use SetView with a LocationRect.

However I found that it always produces to low zoom level, since only integer values was used. If for instance the perfect zoom level would be 5.5, you would get 5.0. To get a proper fit I calculate a new zoom level from TargetZoomLeveland TargetBoundingRectangle:

viewRect = LocationRect.CreateLocationRect(coordinates);
map.SetView(viewRect);
double scale = map.TargetBoundingRectangle.Height/viewRect.Height;
map.ZoomLevel = map.TargetZoomLevel + Math.Log(scale, 2);

This example sets the zoom level to fit viewRect's height on the screen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文