我正在研究一些地图的东西,但我遇到了一些似乎很基本的东西。
我正在做的事情的要点是在地图上添加 10 个标志,我需要根据图钉的位置动态设置地图的缩放级别和中心(地图需要在静止时尽可能放大显示所有引脚)。
这与 MultiMap 的预期完全一样,没有任何干预(如预期的那样),但虚拟地球地图表现不佳。如果我不给它一个中心纬度/经度,它默认显示整个美国。
谁能指出我正确的方向以使其正常工作?
编辑:
好的,我能够通过在我的 MVC 控制器中使用以下内容来计算正确的中心
''# we're going to rock the AVERAGE LAT/LON here
Dim eventCount As Integer = 0
Dim totalLat As Double = 0
Dim totalLon As Double = 0
For Each mel In MapEventList
totalLat += mel.lat
totalLon += mel.lon
eventCount += 1
Next
ViewData("centerLat") = totalLat / eventCount
ViewData("centerLon") = totalLon / eventCount
现在我只是试图计算最佳缩放级别
编辑2:
所以我已经从我的控制器中删除了计算并将其添加到我的 Javascript 中。脚本的重量很轻,但将由客户端计算机而不是我的服务器完成计算(如果/当站点开始获得大量点击时,这很有价值)
$(document).ready(function () {
// Load the Bing Map and populate it
var increment = 0; // used to increment event locations
var sumLat = 0; // used to find the average LAT location
var sumLon = 0; // used to find the average LON location
// Load the initial map
LoadMap();
// loop through all events and place a point on the map
// also add to the sumLat and sumLon variable
$.each(json_object, function () {
increment = ++increment;
sumLat = sumLat + this.lat;
sumLon = sumLon + this.lon;
LoadPoint(this.lat, this.lon, this.title, this.desc, increment);
});
// find the averate Lat and Lon for map centering
var centerLat = sumLat / increment;
var centerLon = sumLon / increment;
LatLon = new VELatLong(centerLat, centerLon);
// Set the new map Center based on the average of all points.
map.SetCenter(LatLon)
});
function LoadMap() {
map = new VEMap('bingMap');
mapOptions = new VEMapOptions
// sets map options for asthetics
mapOptions.DashboardColor = "black";
mapOptions.EnableDashboardLabels = false;
mapOptions.EnableBirdseye = false;
mapOptions.EnableDashboardLabels = false;
// Load the initial map
// note: the LatLon is "null" at this point because we are going to
// center the map later based on the average of all points.
map.LoadMap(null, zoomLevel, null, null, null, false, null, mapOptions);
};
function LoadPoint(lat, lon, title, desc, pinId) {
var point = new VELatLong(lat, lon);
var pin = new VEShape(VEShapeType.Pushpin, point);
pin.SetTitle(title);
pin.SetDescription(desc + "<br /><br />lat: " + lat + " lon: " + lon);
pin.SetCustomIcon("<div class='pinStyle'><div class='pinText'>" + pinId + "</div></div>");
map.AddShape(pin);
};