我正在研究一些地图的东西,而且我遇到了一些似乎非常基本的东西。
我正在做的是为地图添加10个标志,我需要根据引脚的位置动态设置地图的缩放级别和中心(地图需要放大到远处)可能仍然显示所有引脚)。
这与MultiMap完全一样,没有任何干预(正如预期的那样),但Virtual Earth地图效果不佳。如果我没有给它一个中心Lat / Lon,它默认显示所有美国。
任何人都可以指出我正确的方向,以使这个工作?
好的,我可以通过在我的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
现在我只是试图计算最佳缩放级别
所以我从控制器中删除了计算并将其添加到我的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);
};
答案 0 :(得分:3)
如果您使用的是javascript版本,那么您不需要计算它,您可以直接将VELatLong对象数组提供给VEMap.SetMapView,它将居中+缩放以便所有引脚都可见。
答案 1 :(得分:1)
当我遇到类似的问题时,我通过计算(粗略)针脚的质心来解决它。我说“粗糙”因为我只是平均每个点的纬度/经度而这是不正确的,因为地球是一个球体。
答案 2 :(得分:0)
非常感谢@wildpeaks获得答案。我已将结果发布在其他需要朝正确方向推进的人身上。
$(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
var latlonArray = new Array(); // used to create an Array of lat and lon in order to "SetMapView"
// 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 () {
latlonArray[increment] = new VELatLong(this.lat, this.lon); // Add to the array before the increment
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 latlonCenter = new VELatLong(sumLat / increment, sumLon / increment);
// Set the new map Center based on the average of all points.
map.SetCenter(latlonCenter);
map.SetMapView(latlonArray); // pass the latlonArray object to the SetMapView in order to display all map pins appropriately.
});