【问题标题】:How to zoom to specific point in Highmaps如何缩放到 Highmaps 中的特定点
【发布时间】:2020-08-05 17:19:07
【问题描述】:

Highmaps / highcharts 是一个 javascript/jquery 适配器,可以在浏览器等中呈现地图。

我有一张突出显示单个国家/地区的地图,但是,(世界)地图的比例使得我想在地图加载到相关国家/地区后放大。

查看 API,我确信这是可能的。

有一个事件监听器,这样我就可以在加载时执行自定义函数。如本例所示,在加载时添加了一个系列 (Js fiddle)

此外,还有一个方法mapZoom 允许您使用以下参数指定要缩放到的点:

mapZoom (Number howMuch, [Number centerX], [Number centerY], [Number mouseX], [Number mouseY])

但是当我尝试调用这个方法 onload(我的代码在下面尝试,JS fiddle here)时,什么也没有发生。

$(function () {

    $.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=world-population-density.json&callback=?', function (data) {

        // Assign id's
        $.each(data, function () {
            this.id = this.code;
        });

        // Initiate the chart
        $('#container').highcharts('Map', {
            chart: {
                events: {
                    load: function () {
                    mapZoom(0.5, 100, 100);
                    }
                }
            },
            title: {
                text: 'Zoom on load attempt'
            },


            legend: {
                title: {
                    text: 'Population density per km²'
                }
            },

            colorAxis: {
                min: 1,
                max: 1000,
                type: 'logarithmic'
            },

            mapNavigation: {
                enabled: true,
                buttonOptions: {
                    verticalAlign: 'bottom'
                }
            },

            series : [{
                data : data,
                mapData: Highcharts.maps['custom/world-highres'],
                joinBy: ['iso-a2', 'code'],
                name: 'Population density',
                allowPointSelect: true,
                cursor: 'pointer',
                states: {
                    hover: {
                        color: '#BADA55'
                    }
                },
                tooltip: {
                    pointFormat: '{point.id} {point.name}',
                    valueSuffix: '/km²'
                }
            }]
        });

    });
});

【问题讨论】:

    标签: javascript highcharts highmaps


    【解决方案1】:

    mapZoom 是属于 chart 对象的方法,因此,为了将其作为事件 (load) 调用,您必须使用 this 关键字调用它。

    您可以像这样编辑您的代码 (JSFiddle):

    ...
    events: {
        load: function () {
            this.mapZoom(0.5, 100, 100);
            }
        }
    }
    ...
    

    或者,您可以随时使用 jQuery 引用 (JSFiddle) 调用它:

    $('#container').highcharts().mapZoom(0.5, 100, 100);
    

    【讨论】:

    • 如何放大到特定的(纬度、经度)位置?
    • var pos = this.fromLatLonToPoint({ lat: 51.5, lon: 2.0 }); this.mapZoom(0.1, this.xAxis[0].toPixels(pos.x), this.yAxis[0].toPixels(pos.y)); 正在放大,但未到所需位置
    • 您好,放大到 x,y 并不意味着要去 x 纬度,y 经度。该地图具有与地理-90:90、-180:180 不同的坐标系。如果要进行转换,则需要使用 proj4 库。请参阅此讨论:highcharts.uservoice.com/forums/55896-highcharts-javascript-api/…
    【解决方案2】:

    在地图上缩放到特定国家很容易

    series : [{
                ...
                data: [{code: 'HU', id: 'HU', value: 7.5, name: 'Hungary'}],
                ...
                }
    

    ...然后...

    var mapChart=$('#MapContainer').highcharts(); //get map chart object from DOM
    mapChart.get('HU').zoomTo(); //zoom to the country using "id" from data serie
    mapChart.mapZoom(5); //elevate viewpoint a little to see surrounding countries as well
    

    【讨论】:

      【解决方案3】:

      如果你想放大几个国家,你可以试试这个

      events: {
          load: function () {
          var mapChart = this;
          ['DE', 'HU', 'RO'].map(function(code){
              return mapChart.get(code);
          }).reduce(function(acc, current){
            // Set map bounds
            acc._minX = isNaN(acc._minX) ? current._minX : Math.min(acc._minX, current._minX);
            acc._maxX = isNaN(acc._maxX) ? current._maxX : Math.max(acc._maxX, current._maxX);
            acc._minY = isNaN(acc._minY) ? current._minY : Math.min(acc._minY, current._minY);
            acc._maxY = isNaN(acc._maxY) ? current._maxY : Math.max(acc._maxY, current._maxY);
            return acc;
          }).zoomTo(); 
       }}
      

      【讨论】:

      • 我发现您对缩放工作的回答并不准确。您知道需要进行哪些更改吗?
      猜你喜欢
      • 1970-01-01
      • 2021-01-04
      • 2016-02-18
      • 1970-01-01
      • 2016-01-04
      • 1970-01-01
      • 1970-01-01
      • 2020-10-30
      • 2014-08-12
      相关资源
      最近更新 更多