记录:百度地图API BMap实现fitBounds的效果

基于百度API:
1、getViewport
根据提供的地理区域或坐标获得最佳的地图视野,返回的对象中包含center和zoom属性,分别表示地图的中心点和级别。此方法仅返回视野信息,不会将新的中心点和级别做用到当前地图上
2、centerAndZoom
设初始化地图。 如果center类型为Point时,zoom必须赋值,范围3-19级,若调用高清底图(针对移动端开发)时,zoom可赋值范围为3-18级。如果center类型为字符串时,比如“北京”,zoom可以忽略,地图将自动根据center适配最佳zoom级别


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
  body, html,#mapContainer {
    width: 100%;
    height: 100%;
  }
style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=申请的AK">script>
<title>fitBoundstitle>
head>
<body>
<div id="mapContainer">div>
body>
html>
<script type="text/javascript">
const pointList = [ [ 119.144220126, -22.772636356, 0.0 ], [ 119.119609823, -22.701267344, 0.0 ], [ 119.096194593, -22.706476568, 0.0 ], [ 118.983959285, -22.706789256, 0.0 ], [ 118.969747012, -22.713100862, 0.0 ], [ 118.965776174, -22.720474664, 0.0 ], [ 118.848105524, -22.928705224, 0.0 ], [ 118.886336774, -22.958404317, 0.0 ], [ 119.045742202, -22.93542713, 0.0 ], [ 119.144220126, -22.772636356, 0.0 ] ]
const resultPoints = pointList
  .map((p) => {
      return new BMap.Point(p[0], p[1])
  })
const map = new BMap.Map("mapContainer")
map.enableScrollWheelZoom(true)
fitBounds(resultPoints)

/**
 * 自适应到容纳当前所有点位的合适的bounds和zoom
 * @param  {[type]} bPoints [description]
 * @return {[type]}         [description]
 */
function fitBounds (bPoints) {
  const { zoom, center } = map.getViewport(eval(bPoints))
  map.centerAndZoom(center,zoom)
}
script>

注:百度API

你可能感兴趣的:(百度地图API,BMap)