这个
mapView.fitToBounds()
该方法使用投影值,而不是您所描述的像素值。您可以使用以下命令将地理坐标转换为投影平面值
lonLatToProjectedUnits()
转换。
const chartComponent = useRef(null);
const [options] = useState({
chart: {
map: usMap,
},
mapNavigation: {
enabled: true,
buttonOptions: {
alignTo: "spacingBox",
},
},
series: [
{
name: "US",
dataLabels: {
enabled: true,
format: "{point.properties.hc-a2}",
},
},
{
name: "map bubble",
type: "mapbubble",
color: "rgb(206, 0, 0, 0.7)",
maxSize: "10%",
data: [
{
geometry: {
coordinates: [-121.2597459908211, 47.03439487299908],
type: "Point",
},
z: 1,
},
{
geometry: {
coordinates: [-102.04591594861265, 42.72721793991439],
type: "Point",
},
z: 5,
},
],
},
],
});
useEffect(() => {
if (chartComponent && chartComponent.current) {
const mapView = chartComponent.current.chart.mapView;
const points = chartComponent.current.chart.series[1].points;
const min = mapView.lonLatToProjectedUnits({
lon: points[1].geometry.coordinates[0],
lat: points[1].geometry.coordinates[1],
});
const max = mapView.lonLatToProjectedUnits({
lon: points[0].geometry.coordinates[0],
lat: points[0].geometry.coordinates[1],
});
mapView.fitToBounds({
x1: min.x,
x2: max.x,
y1: min.y,
y2: max.y
}, '10%')
}
}, [chartComponent]);
return (
<HighchartsReact
highcharts={Highcharts}
constructorType={"mapChart"}
options={options}
ref={chartComponent}
/>
);
演示:
https://codesandbox.io/p/sandbox/highcharts-react-demo-forked-mjq55k?file=%2Fdemo.jsx%3A47%2C13