Virtual Earth / Bing Maps - How To Calculate Best Zoom Level

左心房为你撑大大i 提交于 2019-12-06 15:52:17

If you are using the javascript version, then you don't need to calculate it, you can directly give the array of VELatLong objects to VEMap.SetMapView, and it will center + zoom so that all pins are visible.

when I had similar problem I solved it with calculating (rough) center of mass for the pins. I say "rough" because I just averaged out the lat/lon for each point and that's not correct as the Earth is a sphere.

Chase Florell

Thanks so much @wildpeaks for the answer. I've posted my results here for others who need a nudge in the right direction.

$(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.

});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!