在官方示例中setFitView()对AMap.Marker可以生效,但是对海量点标记 AMap.MassMarks 貌似不行,所以搞了另一种方法;
Map类有一个方法setBounds也能实现重新调整视角
https://lbs.amap.com/api/javascript-api/reference/map
const sw = getSW(markerList) // 循环所有的点标记,返回最西南的一个经纬度
const ne = getNE(markerList) // 循环所有的点标记,返回最东北的一个经纬度
console.log('GET BOUNDS', sw, ne) // [xxx,xxx], [xxx,xxx]
let mybounds = new AMap.Bounds(sw, ne)
this.map.setBounds(mybounds)
以下是自定义的函数,返回最西南、最东北坐标,自行修改
/**
* 坐标集合的最西南角
* @param {*} list
* list 是接口获取的点 的数组
*/
export const getSW = (list) => {
let south = null
let west = null
for (let item of list) {
if ((west && item.longitude < west) || !west) {
west = item.longitude - 0.7
}
if ((south && item.latitude < south) || !south) {
south = item.latitude - 0.7
}
}
return [west, south]
}
/**
* 最东北角
* @param {*} list
*/
export const getNE = (list) => {
let north = null
let east = null
for (let item of list) {
if ((east && item.longitude > east) || !east) {
east = item.longitude + 0.7
}
if ((north && item.latitude > north) || !north) {
north = item.latitude + 0.7
}
}
return [east, north]
}