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

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

<!DOCTYPE html>
<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>fitBounds</title>
</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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值