百度地图默认最大缩放级别为19级,但在某些场景中需要结合自定义贴图实现更高级别的视图
通过下面提供的两种方式就能自定义最大最小缩放层级
缩放到20级的效果图
方法一:通过自定义瓦片迂回方式设置
提示:请使用自己申请的《百度地图key》替换代码中的《此处使用你自己的key》
<!DOCTYPE html>
<html><head><meta charset="utf-8" /><title>百度地图突破19级缩放限制</title><style>html,body,.map {height: 100%;margin: 0;}.map-tools {position: absolute;right: 20px;top: 10px;z-index: 10;padding: 5px 8px;background-color: #fff;border-radius: 2px;font-size: 14px;box-shadow: rgba(0, 0, 0, 0.35) 2px 2px 3px;}.zoom-less,.zoom-plus {border: 1px solid #ddd;}.zoom-num { padding: 0 5px;}</style></head><body><div class="map-tools"><span>缩放范围:15~25  </span> <button class="zoom-less" onclick="mapZoom('less')">-</button><strong class="zoom-num">0</strong><button class="zoom-plus" onclick="mapZoom('plus')">+</button></div><div class="map" id="map"></div><script src="https://api.map.baidu.com/api?v=3.0&ak=此处使用你自己的key"></script><script>const map = new BMap.Map('map');const defaultMapType = map.getMapType();const defaultTileLayer = defaultMapType.getTileLayer();const newMapType = new BMap.MapType('新地图', defaultTileLayer, { minZoom: 15, maxZoom: 25 });const zoomNum = 15;const zoomNumDom = document.querySelector('.zoom-num');map.setMapType(newMapType);map.centerAndZoom(new BMap.Point(116.404, 39.915), zoomNum);map.addControl(new BMap.ScaleControl()); //添加比例尺map.enableScrollWheelZoom(true); //激活鼠标滚轮缩放map.addEventListener('zoomend', setMapZoomText); //监听地图缩放setMapZoomText();//设置缩放级别文字function setMapZoomText() {var zoom = map.getZoom();zoomNumDom.innerText = zoom;}//设置缩放级别function mapZoom(type) {var zoom = map.getZoom();if (type==='less') {zoom--;} else {zoom++;} map.setZoom(zoom);}</script></body>
</html>
方法二:通过重置百度地图的全局变量(百度地图3.0版本)
提示:请使用自己申请的《百度地图key》替换代码中的《此处使用你自己的key》
2021-03-16更新:
<!DOCTYPE html>
<html><head><meta charset="utf-8" /><title>百度地图突破19级缩放限制</title><style>html,body,.map {height: 100%;margin: 0;}.map-tools {position: absolute;right: 20px;top: 10px;z-index: 10;padding: 5px 8px;background-color: #fff;border-radius: 2px;font-size: 14px;box-shadow: rgba(0, 0, 0, 0.35) 2px 2px 3px;}.zoom-less,.zoom-plus {border: 1px solid #ddd;}.zoom-num {padding: 0 5px;}</style>
</head><body><div class="map-tools"><span>缩放范围:15~25  </span><button class="zoom-less" onclick="mapZoom('less')">-</button><strong class="zoom-num">0</strong><button class="zoom-plus" onclick="mapZoom('plus')">+</button></div><div class="map" id="map"></div><script src="https://api.map.baidu.com/api?v=3.0&ak=此处使用你自己的key"></script><script>const maxZoom = 25; //最大层级window.BMAP_NORMAL_MAP.m.u4 = window.BMAP_NORMAL_MAP.m.qc = window.BMAP_NORMAL_MAP.m.maxZoom = window.BMAP_PERSPECTIVE_MAP.m.u4 = window.BMAP_PERSPECTIVE_MAP.m.qc = window.BMAP_PERSPECTIVE_MAP.m.maxZoom = window.BMAP_SATELLITE_MAP.m.u4 = window.BMAP_SATELLITE_MAP.m.qc = window.BMAP_SATELLITE_MAP.m.maxZoom = window.BMAP_HYBRID_MAP.m.u4 = window.BMAP_HYBRID_MAP.m.qc = window.BMAP_HYBRID_MAP.m.maxZoom = maxZoom; const map = new BMap.Map('map', { minZoom: 15 });const zoomNum = 15;const zoomNumDom = document.querySelector('.zoom-num');map.centerAndZoom(new BMap.Point(116.404, 39.915), zoomNum);map.addControl(new BMap.ScaleControl());map.enableScrollWheelZoom(true);map.addEventListener('zoomend', setMapZoomText);setMapZoomText();//设置缩放级别文字function setMapZoomText() {var zoom = map.getZoom();zoomNumDom.innerText = zoom;}//设置缩放级别function mapZoom(type) {var zoom = map.getZoom();if (type === 'less') {zoom--;} else {zoom++;}map.setZoom(zoom);}</script>
</body></html>
历史版本:
<!DOCTYPE html>
<html><head><meta charset="utf-8" /><title>百度地图突破19级缩放限制</title><style>html,body,.map {height: 100%;margin: 0;}.map-tools {position: absolute;right: 20px;top: 10px;z-index: 10;padding: 5px 8px;background-color: #fff;border-radius: 2px;font-size: 14px;box-shadow: rgba(0, 0, 0, 0.35) 2px 2px 3px;}.zoom-less,.zoom-plus {border: 1px solid #ddd;}.zoom-num {padding: 0 5px;}</style>
</head><body><div class="map-tools"><span>缩放范围:15~25  </span><button class="zoom-less" onclick="mapZoom('less')">-</button><strong class="zoom-num">0</strong><button class="zoom-plus" onclick="mapZoom('plus')">+</button></div><div class="map" id="map"></div><script src="https://api.map.baidu.com/api?v=3.0&ak=此处使用你自己的key"></script><script>const maxZoom = 25; //最大层级window.BMAP_NORMAL_MAP.m.X3 = window.BMAP_NORMAL_MAP.m.mc = window.BMAP_NORMAL_MAP.m.maxZoom = window.BMAP_PERSPECTIVE_MAP.m.X3 = window.BMAP_PERSPECTIVE_MAP.m.mc = window.BMAP_PERSPECTIVE_MAP.m.maxZoom = window.BMAP_SATELLITE_MAP.m.X3 = window.BMAP_SATELLITE_MAP.m.mc = window.BMAP_SATELLITE_MAP.m.maxZoom = window.BMAP_HYBRID_MAP.m.X3 = window.BMAP_HYBRID_MAP.m.mc = window.BMAP_HYBRID_MAP.m.maxZoom = maxZoom; const map = new BMap.Map('map', { minZoom: 15 });const zoomNum = 15;const zoomNumDom = document.querySelector('.zoom-num');map.centerAndZoom(new BMap.Point(116.404, 39.915), zoomNum);map.addControl(new BMap.ScaleControl());map.enableScrollWheelZoom(true);map.addEventListener('zoomend', setMapZoomText);setMapZoomText();//设置缩放级别文字function setMapZoomText() {var zoom = map.getZoom();zoomNumDom.innerText = zoom;}//设置缩放级别function mapZoom(type) {var zoom = map.getZoom();if (type === 'less') {zoom--;} else {zoom++;}map.setZoom(zoom);}</script>
</body></html>