1

我正在研究一些地图的东西,但我遇到了一些似乎很基本的东西。

我正在做的事情的要点是在地图上添加 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);
};

但是我仍然坚持尝试计算最佳缩放级别

4

3 回答 3

3

如果您使用的是 javascript 版本,则无需计算,可以直接将 VELatLong 对象数组传递给VEMap.SetMapView,它会居中 + 缩放,以便所有引脚可见。

于 2010-11-10T16:16:22.923 回答
1

当我遇到类似问题时,我通过计算(粗略)销的质心来解决它。我说“粗糙”是因为我只是平均了每个点的纬度/经度,这是不正确的,因为地球是一个球体。

于 2010-11-06T21:12:16.517 回答
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.

});
于 2010-11-10T20:57:13.417 回答