在 jvectormap 中点击事件自动缩放区域

Auto zoom a region on click event in jvectormap

我有Australia的jvectormap。单击地图中的特定状态时,它应该在同一地图中缩小该特定状态。有什么办法可以不使用multi-map实现吗

是的,当然 - 你是说放大?使用地图对象的setFocus方法:

  onRegionClick: function(e,  code,  isSelected,  selectedRegions){
    $('#map').vectorMap('get','mapObject').setFocus({region: code});
  }

编辑:

此处记录了 setFocus 方法:jVectorMap API Documentation - Map

这是 setFocus 的摘录(来源:Kirill Lebedev,jVectorMap 的伟大作者):

  /**
   * setFocus set the map's viewport to the specific point and set zoom of the map 
      to the specific level. Point and zoom level could be defined 
      in two ways: using the code of some region to focus on or a central point
      and zoom level as numbers.
   * @param This method takes a configuration object as the single argument. 
     The options passed to it are the following:
   *   @param {Array} params.regions Array of region codes to zoom to.
   *   @param {String} params.region Region code to zoom to.
   *   @param {Number} params.scale Map scale to set.
   *   @param {Number} params.lat Latitude to set viewport to.
   *   @param {Number} params.lng Longitude to set viewport to.
   *   @param {Number} params.x Number from 0 to 1 specifying the horizontal 
        coordinate of the central point of the viewport.
   *   @param {Number} params.y Number from 0 to 1 specifying the vertical 
        coordinate of the central point of the viewport.
   *   @param {Boolean} params.animate Indicates whether or not to animate 
        the scale change and transition.
   */

有些选项是相互排斥的,例如,如果您提供纬度和经度,显然 x 和 y 值(以像素为单位的地图坐标)将被忽略。 此外:同样,如果您提供区域代码或区域代码、纬度和经度以及x和y值,它们都将被忽略。

要得到动画效果,需要给参数对象提供选项animate: true

示例:

$('#map').vectorMap('get','mapObject').setFocus({region: code, animate: true});

为了获得漂亮流畅的效果,您应该使用 scale 参数,这是因为用户可以放大地图,然后单击一个区域,然后,由于这个原因,对该区域的缩放效果不会像预期的那样明显。或其他任何东西,也取决于您的初始缩放级别。

因此,您可以研究用户界面,规划用户交互,并使用缩放选项进行一些测试以获得所需的效果(抱歉,我无能为力,因为您没有提供任何源代码)。

从这里开始,尝试得到你最终想要的动画效果:

// Zoom in to the Eyers Rock:
var zParams = {scale: 5, lat: -25.360790, lng: 131.016800, x: 0.5, y: 0.5, animate: true};
$('#map').vectorMap('get','mapObject').setFocus(zParams);

我发现 FocusOn{scale - 可以很好地构建您的选择。您可以使用此命令随意前进或后退。

$(function(){
          // var $ = jQuery;
          $('#EUmap').vectorMap({
            map: 'europe_en',
            panOnDrag: true,
            focusOn: {
              x: 0.5,
              y: 0.5,
              scale: 1.5,
              animate: true
            },
            series: {
              regions: [{
                scale: ['#C8EEFF', '#0071A4'],
                normalizeFunction: 'polynomial',
                values: {
                  "BR": 2023.53,
                  "CZ": 195.23,
                  "DM": 0.38,
                  "FR": 2555.44,
                  "DE": 3305.9,
                  "GR": 305.01,
                  "HU": 132.28,
                  "IT": 2036.69
                }
              }]
            }
          });
        })
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jvectormap@2.0.4/jquery-jvectormap.min.css" type="text/css">
<html>

<head>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jvectormap@2.0.4/jquery-jvectormap.min.css" type="text/css">
  <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/jvectormap@2.0.4/jquery-jvectormap.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jqvmap/1.5.1/maps/continents/jquery.vmap.europe.js"></script>
</head>

<body>
  <div id="EUmap" style="width: 600px; height: 400px"></div>
</body>

</html>

您可以在生成对象属性 focusOn 中使用值(国家代码),示例:

focusOn: "UA",

它适用于初始缩放,适用于任何缩放使用方法setFocus

缩放和其他文档:https://jvectormap.com/documentation/javascript-api/jvm-map/