[英]How to zoom a bing map to display a set of lat/long points?
I'm using the Bing Maps v7 control. 我正在使用Bing Maps v7控件。
I have an array of latitude/longitude points. 我有一系列纬度/经度点。 (of type
Microsoft.Map.Location
) (类型为
Microsoft.Map.Location
)
From that array, I can generate a rectangle, and a center of the rectangle, using LocationRect.fromLocations()
从该数组,我可以使用
LocationRect.fromLocations()
生成矩形和矩形的中心
When creating a map with the Map constructor, I can center the map using the center of that rectangle. 使用Map构造函数创建地图时,我可以使用该矩形的中心来居中地图。 It looks something like this:
它看起来像这样:
var LLA = [
new Microsoft.Maps.Location(43.386,-111.6123),
new Microsoft.Maps.Location(43.4929, -112.0349),
new Microsoft.Maps.Location(43.2609,-115.7811),
...
];
var r = Microsoft.Maps.LocationRect.fromLocations(LLA);
var mapOptions = {
credentials : bingMapsKey,
center : r.center,
mapTypeId : Microsoft.Maps.MapTypeId.road,
zoom : 7
},
elt = document.getElementById('out2');
var map = new Microsoft.Maps.Map(elt, mapOptions);
That works, and the map that gets created is centered around those points. 这是有效的,创建的地图以这些点为中心。 How can I set the zoom level of the Bing Map so that it displays the entire rectangle?
如何设置Bing Map的缩放级别,以便显示整个矩形?
See here: http://jsfiddle.net/MQ76x/ 见这里: http : //jsfiddle.net/MQ76x/
Forget passing center
/ zoom
, and pass the rectangle as bounds
instead : 忘记传递
center
/ zoom
,并将矩形作为bounds
传递 :
var mapOptions = {
credentials : bingMapsKey,
bounds : r, // <-- HERE
mapTypeId : Microsoft.Maps.MapTypeId.road
}
For anyone landing here as me and searching for a calculation for the zoom for BingMaps V8 depending on the center of a location (latitude, longitude) and the radius of the location in kilometers: 对于任何以我身份登陆并根据位置中心(纬度,经度)和以千米为单位的位置半径搜索BingMaps V8的缩放计算的人:
(Maybe you are also drawing a circle with the Microsoft.Maps.SpatialMath
at the center of the location) (也许你也在该位置的中心绘制一个带有
Microsoft.Maps.SpatialMath
的圆圈)
function getZoomLevel(radiusInKilometer: number, latitude: number, heightOfMapInPixels: number, widthOfMapInPixels: number): number {
const rangeInMeters = radiusInKilometer * 1000;
// we take the lower value of the bounding rect of the map, so the circle will be best seen
const limitBoundPixels = Math.min(heightOfMapInPixels, widthOfMapInPixels);
const zoom = Math.floor(Math.log(156543.03392 * Math.cos(latitude * Math.PI / 180) / (rangeInMeters / limitBoundPixels)) / Math.log(2));
return zoom;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.