block by wboykinm 9942798

get map extent as static image with bounding box vector

Full Screen

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Leaflet Image</title>

<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox.js/v1.6.2/mapbox.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox.js/v1.6.2/mapbox.css' rel='stylesheet' />

<style>
  body { margin:0; padding:0; }
  #map { position:absolute; top:0; bottom:0; width:100%; }
  #snap {position: absolute; bottom: 10px; left: 10px; z-index: 99;}
  #extentMap {width:300px;height:200px;position:absolute;z-index:98;top:10px;right:10px;}
</style>
</head>
<body>


    <script src='//api.tiles.mapbox.com/mapbox.js/plugins/leaflet-image/v0.0.3/leaflet-image.js'></script>

    <button id='snap' onClick='doImage()'>Map Snapshot</button>
    <div id='map' style='width: 70%;'></div>
    <div id='extentMap'></div>

    <script type="text/javascript">
        var map = L.mapbox.map('map', 'faraday2.hi7p6kfl', {
            center: [44.5, -73.2],
            zoom: 12
        });

        function doImage() {
            var extentMap = L.mapbox.map('extentMap', 'faraday2.hi7p6kfl', {
                zoomControl: false,
                infoControl: false,
                keyboard: false,
                center: map.getCenter(),
                zoom: map.getZoom() - 2
            });

            extentMap.dragging.disable();
            extentMap.touchZoom.disable();
            extentMap.doubleClickZoom.disable();
            extentMap.scrollWheelZoom.disable();
            if (extentMap.tap) extentMap.tap.disable();
            console.log(map.getBounds()._northEast.lat);
            var extentPoly = {
                "type": "Feature",
                "properties": {
                    "name": "viewport"
                },
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [[
                        [ map.getBounds()._northEast.lng, map.getBounds()._northEast.lat ],
                        [ map.getBounds()._northEast.lng, map.getBounds()._southWest.lat ],
                        [ map.getBounds()._southWest.lng, map.getBounds()._southWest.lat ],
                        [ map.getBounds()._southWest.lng, map.getBounds()._northEast.lat ],
                        [ map.getBounds()._northEast.lng, map.getBounds()._northEast.lat ]
                    ]]
                }
            };
            console.log(extentPoly);
            L.geoJson(extentPoly, {
                style: function (feature) {
                    return {
                      color: '#49ada6',
                      weight: 3,
                      fillColor: '#e9d362',
                      opacity: 0.8,
                      fillOpacity: 0.3,
                      clickable: false
                    };
                }
            }).addTo(extentMap);
        }
    </script>


</body>
</html>