geo: [
{
map: 'xxx',
animationDurationUpdate: 0, // 建议添加 拖拽更丝滑
roam: true, // 允许拖动
layoutCenter: ['50%', '50%'],
layoutSize: '100%',
silent: true,
scaleLimit: {
min: 1,
max: 6,
},
},
{
map: 'xxxx',
animationDurationUpdate: 0, // 建议添加 拖拽更丝滑
roam: true, // 允许拖动
layoutCenter: ['50%', '50%'],
layoutSize: '100%',
silent: true,
scaleLimit: {
min: 1,
max: 6,
},
},
// 监听缩放和拖拽事件
myChart.on('geoRoam', (params) => {
const option = myChart.getOption()
const geoId = params.geoId // 获取当前操作的 geoId
// 从 geoId 中提取索引
const currentGeoIndex = parseInt(geoId.split('\u0000series\u0000')[1].split('\u0000')[0]) // 找到变更的geo下标
if (params.zoom != null && params.zoom != undefined) {
for (let index = 0; index < option.geo.length; index++) {
const element = option.geo[index]
if (index !== currentGeoIndex) {
element.zoom = option.geo[currentGeoIndex].zoom
element.center = option.geo[currentGeoIndex].center
}
}
myChart.dispatchAction({
type: 'restore',
})
} else {
const baseMapGeo = option.geo[currentGeoIndex]
const zoom = option.geo[currentGeoIndex].zoom
const center = baseMapGeo.center || ['50%', '50%']
const dx = params.dx || 0 // 获取水平拖拽距离
const dy = params.dy || 0 // 获取垂直拖拽距离
const newCenter = [parseFloat(center[0]) + dx, parseFloat(center[1]) + dy]
for (let index = 0; index < option.geo.length; index++) {
const element = option.geo[index]
if (index !== currentGeoIndex) {
element.center = newCenter // 更新其他 geo 的 center
}
}
}
myChart.setOption(option)
})