locked
Get what zoom level map.setView({ bounds: bounds }) will use before executing. Or equivalent of map.getZoom({ bounds: bounds }) RRS feed

  • Question

  • I am looking for a something like getZoom({ bounds: bounds }).

    I need to to know the zoom level before it is set so I can over-ride it with a different zoom level if needed. Basically I need this:

    map.setView({ zoom: Math.min(14, map.getZoom({ bounds: bounds }) });

    Is there a way to peek at what setView will do before it does it or is there a way to find out what zoom level a polygon or bounds object needs?

    Setting the zoom level, waiting for it to actually happen so I can get the real zoom level, and then changing it if I don't like it is a kludgy, slow and unreliable method of accomplishing this.

    If there is no direct way to do it, does someone have some code on how to calculate this if I know the map size?

    Thanks,

    Brad

    Monday, April 15, 2013 5:44 PM

Answers

All replies

  • There is no way to peak at what SetView will set the view to, however you can calculate the best map view for the bounding box which will likely be the same value as what the set view method will use. Take a look at this blog post for how to calculate this: http://rbrundritt.wordpress.com/2009/07/21/determining-best-map-view-for-an-array-of-locations/

    http://rbrundritt.wordpress.com

    • Proposed as answer by Ricky_Brundritt Monday, April 15, 2013 7:49 PM
    • Marked as answer by Brad Mathews Monday, April 15, 2013 11:00 PM
    Monday, April 15, 2013 7:49 PM
  • Richard,

    Exactly what I was looking for. Thanks! I am attaching my javascript implementation of your Silverlight code for others.  I have also added some functionality that was useful to my application. The extra junk can be stripped if you do not need it.

        function getZoom(map, params) {      // params are: locations, bounds, mapWidth, mapHeight, buffer
    
            var maxLat = -85,
                minLat = 85,
                maxLon = -180,
                minLon = 180,
                zoom1 = 0,
                zoom2 = 0,
                i;
    
            // If there are no params, then just get the zoom level of the passed map.
            if (!params) {
                return map.getZoom();
            }
    
            // If we have a locations array, the we ned to get the MBR
            if (params.locations) {     // A list of location objects, we need to get MBR
                for (i = 0; i < params.locations.length; i++) {
                    if (params.locations[i].latitude > maxLat) {
                        maxLat = params.locations[i].latitude;
                    }
                    if (params.locations[i].latitude < minLat) {
                        minLat = params.locations[i].latitude;
                    }
                    if (params.locations[i].longitude > maxLon) {
                        maxLon = params.locations[i].longitude;
                    }
                    if (params.locations[i].longitude < minLon) {
                        minLon = params.locations[i].longitude;
                    }
                }
            } else if (params.bounds) {     // if we have a bounds object, then it is an MBR already
                minLat = bounds.getSouth();
                maxLat = bounds.getNorth();
                minLng = bounds.getEast();
                maxLng = counds.getWest();
            } else {
                return map.getZoom();
            }
    
            //Determine the best zoom level based on the map scale and bounding coordinate information
            if (maxLon != minLon && maxLat != minLat)
            {
                //best zoom level based on map width
                zoom1 = Math.log(360.0 / 256.0 * (params.mapWidth - 2 * params.buffer) / (maxLon - minLon)) / Math.log(2);
                //best zoom level based on map height
                zoom2 = Math.log(180.0 / 256.0 * (params.mapHeight - 2 * params.buffer) / (maxLat - minLat)) / Math.log(2);
            }
    
            //use the most zoomed out of the two zoom levels
            return (zoom1 < zoom2) ? zoom1 : zoom2;
        }

    Usage is:

        params = {
            locations: polygon.getLocations(), // An array of locations, or can use bounds: boundsObject
            mapWidth: width,                   // Width of your map div in pixels
            mapHeight: height,                 // Height of your map div in pixels
            buffer: 0                          // How many pixels to add as a buffer
        };
        zoom = getZoom(map, params);           // map is a current Microsoft.Maps.Map. Can pass null here if you are using params.
    - Brad



    Monday, April 15, 2013 11:21 PM