FunnyShoes
Posts: 6
Joined: Thu May 26, 2022 1:25 pm

How to auto zoom by default only fitting in the non empty map area?

Mon Mar 13, 2023 5:30 pm

I'm trying to show a map, which by default zooms in such that only and all of the points are fitted in the map.

For example if I were showing a map of the world and I only have two points in California, upon load the map would auto zoom into California.

I've attached two images showing a similar example where upon load I'm currently showing the united states. I want the default zoom to look like the second zoomed in image.

I'm using Highcharts v10.3.3 and developing in React.

Code: Select all

const options = {
    title: undefined,
    legend: {
      enabled: false,
    },

    fitDataOnLoad: true,
    mapNavigation: {
      enabled: false,
      enableButtons: false,
      enableDoubleClickZoom: false,
      enableMouseWheelZoom: true,
      enableTouchZoom: false,
      enableDoubleClickZoomTo: false,
    },

    chart: {
      panning: {
        enabled: true,
        type: 'xy',
      },
      proj4,
    },
    tooltip: {
      pointFormat: '{point.name}: <b>34</b>',
      borderColor: 'black',
    },
    plotOptions: {
      mappoint: {
        dataLabels: {
          enabled: true,
          shape: 'mapmarker',
          style: {
            textOutline: 'none',
            fontWeight: 'bold',
            fontSize: '13px',
          },
          y: -13,
        },
        marker: {
          symbol: 'mapmarker',
          lineWidth: 1,
        },
      },
    },

    series: [
      {
        mapData: mapDataUs,
        showInLegend: false,
      },
      {
        type: 'mappoint',
        name: 'Cities',
        states: {
          inactive: {
            opacity: 0.2,
          },
        },
        data: [
          {
            name: 'New York',
            lat: 40.7128,
            lon: -74.006,
            y: 9,
          },
          /*{
            name: 'Los Angeles',
            lat: 34.0522,
            lon: -118.2437,
            y: 9,
          },*/
          {
            name: 'Chicago',
            lat: 41.8781,
            lon: -87.6298,
            value: 40,
            y: 6,
          },
        ],
        dataLabels: {
          format: '{y}',
          style: {
            color: 'white',
          },
          shape: 'mapmarker',
          borderColor: 'black',
          borderWidth: 1,
        },
      },
    ],
  }

FunnyShoes
Posts: 6
Joined: Thu May 26, 2022 1:25 pm

Re: How to auto zoom by default only fitting in the non empty map area?

Mon Mar 13, 2023 5:57 pm

Here's the screenshots, I guess they weren't uploaded
Screen Shot 2023-03-13 at 1.13.47 PM.png
Screen Shot 2023-03-13 at 1.13.47 PM.png (52.73 KiB) Viewed 585 times
Screen Shot 2023-03-13 at 1.30.03 PM.png
Screen Shot 2023-03-13 at 1.30.03 PM.png (74.22 KiB) Viewed 585 times


FunnyShoes
Posts: 6
Joined: Thu May 26, 2022 1:25 pm

Re: How to auto zoom by default only fitting in the non empty map area?

Tue Mar 14, 2023 2:47 pm

I don't see this has been posted anywhere, I've searched for a bit and have been looking at this for a few days already

jedrzej.r
Posts: 519
Joined: Tue Jan 24, 2023 11:21 am

Re: How to auto zoom by default only fitting in the non empty map area?

Wed Mar 15, 2023 9:14 am

Hello!

Welcome to our forum and thanks for contacting us with your question!

You can achieve described effect by making use of mapView.fitToBounds function, which takes bounds as projected units and zooms the map to these bounds.

Take a look at this demo: https://jsfiddle.net/BlackLabel/64oumjqk/.

References:
https://api.highcharts.com/class-refere ... ectedUnits
https://api.highcharts.com/class-refere ... itToBounds

Let me know if this was what you were looking for!
Best regards!
Jędrzej Ruta
Highcharts Developer

FunnyShoes
Posts: 6
Joined: Thu May 26, 2022 1:25 pm

Re: How to auto zoom by default only fitting in the non empty map area?

Thu Mar 16, 2023 4:54 pm

Thanks, just fyi for posterity this fitToBounds was the solution.

jedrzej.r
Posts: 519
Joined: Tue Jan 24, 2023 11:21 am

Re: How to auto zoom by default only fitting in the non empty map area?

Tue Mar 21, 2023 1:28 pm

Glad I could help!

In case of any other questions, feel free to contact us anytime.
Best regards!
Jędrzej Ruta
Highcharts Developer

Return to “Highcharts Maps”