Calculate Tile Bounds

This example uses some of the Tile math functions in the Spatial Math library. The Tile math calculations are useful when creating custom visualization (i.e. heat maps). In this example every time the map has moved all the tiles that are in the current map view are calculated. A polygon is then generated for each one to show its area and a pushpin, with its title property set to the quadkey value of the tile, is placed in the center of the polygon.

<!DOCTYPE html>
    <meta charset="utf-8" />
	<script type='text/javascript'>
    var map;

    function GetMap() {
        map = new Microsoft.Maps.Map('#myMap', {
            credentials: 'Your Bing Maps Key',
            zoom: 4

        //Load the Spatial Math module.
        Microsoft.Maps.loadModule("Microsoft.Maps.SpatialMath", function () {

            //Recaulte the map tiles bounds everytime the map moves.
            Microsoft.Maps.Events.addHandler(map, 'viewchangeend', showMapTileBounds);

    function showMapTileBounds() {

        //Get a list of all the tiles in the current map view.
        var tiles = Microsoft.Maps.SpatialMath.Tiles.getTilesInBounds(map.getBounds(), map.getZoom());

        for (var i = 0; i < tiles.length; i++) {
            //Calculate the bounding rectangle of the tile.
            var tileLocRect = Microsoft.Maps.SpatialMath.Tiles.tileToLocationRect(tiles[i]);

            //Create a polygon for the tile and add it to the map.
            var poly = Microsoft.Maps.SpatialMath.locationRectToPolygon(tileLocRect);

            //Add a pushpin with the quadkey value set as the title. 
            var pin = new Microsoft.Maps.Pushpin(, {
                title: tiles[i].quadKey
    <script type='text/javascript' src='' async defer></script>
    <div id="myMap" style="position:relative;width:600px;height:400px;"></div>

Running this code will load the map and display a polygon and pushpin above each map tile in the current map view.