jvectormap仅显示特定区域的标签

问题描述 投票:0回答:1

我正在尝试显示我在地图上突出显示的唯一区域的标签。

这是代码:

var map = $('#map').vectorMap({
    regionsSelectable: true,
    regionsSelectableOne: true,
    series: {
        regions: [{
            scale: ['#cccccc', '#0A6EB4'],
            values: data
        }]
    },
    regionStyle: {
        initial: {
            fill: '#ffffff',
            "fill-opacity": 1,
            stroke: '#cccccc',
            "stroke-width": 0,
            "stroke-opacity": 1
        },
        hover: {
            "fill-opacity": 1,
            cursor: 'pointer'
        },
        selected: {
            fill: '#0A6EB4'
        },
        selectedHover: {
            fill: '#1E4669'
        }
    },
    regionLabelStyle: {
        initial: {
            'font-family': 'Verdana',
            'font-size': '12',
            'font-weight': 'bold',
            cursor: 'default',
            fill: 'black'
        },
        hover: {
            cursor: 'pointer'
        }
    },
    labels: {
        regions: {
            render: function (code) {
                if (activeCountries.hasOwnProperty(code)) {
                    var regions = $("#map").vectorMap("get", "mapObject").regions;
                    console.log(code, regions[code]); //<< ERROR!
                }
            }
        }
    }
});

在labels.regions.render函数中,我能够获取该国家/地区的代码,我能够验证数组activeCountries中存在的国家/地区,但是当我尝试检索国家/地区名称时(区域[代码] ] .config.name)区域[code]未定义,缺少具体代码!

那么我可以在activeCountries数组中出现的区域上显示标签吗?

这就是我想要实现的目标:

enter image description here

上面的图像是使用Highmaps完成的。

更新感谢deblocker找到了解决方案。

现在我只需要找到一种方法来为区域文本添加白色阴影,以便在黑暗突出显示的区域上更好地显示。

javascript jvectormap
1个回答
1
投票

所以,我相信你正在使用世界地图,这是正确的吗?

有些国家实在太小,无法在世界地图上以有意义的方式绘制SVG路径。

你可以:

  • 使用分辨率更高的地图(显然放大直到看到那些很小的区域)
  • 添加标记以轻松找到这些小国家: { "BH": {"latLng": [26.066700, 50.557700], "name": "Bahrain"}, "GI": {"latLng": [36.140751, -5.353585], "name": "Gibraltar"}, "HK": {"latLng": [22.284681,114.158177], "name": "Hong Kong"}, "MQ": {"latLng": [14.641528,-61.024174], "name": "Martinique"}, "MT": {"latLng": [35.937496, 14.375416], "name": "Malta"}, "MU": {"latLng": [-20.183590,57.941208], "name": "Mauritius"}, "SG": {"latLng": [1.352083, 103.819836], "name": "Singapore"}, "GP": {"latLng": [16.265000,-61.551000], "name": "Guadeloupe"} }


If You need the region names for the regions in the map, You can get it from the loaded maps:

这是演示:

$(document).ready(function () {
  var map = "world_mill_en",
      regions = {"MN": "#fad"};
  $("#map").vectorMap({
    map: map,
    series: {
      regions: [{
        values: regions,
        attribute: "fill"
      }]
    },
    labels: {
      regions: {
        render: function(code){
          return regions[code] && jvm.Map.maps[map].paths[code].name;
        }
      }
    }
  });
});
<html>
<head>
  <title>jVectorMap Labels</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/jquery-jvectormap.min.css" type="text/css">
  <style>
    .jvectormap-region.jvectormap-element {
      text-shadow: -1px -1px 3px #fff, 1px -1px 3px #fff, -1px 1px 3px #fff, 1px 1px 3px #fff;
    }
  </style>
  <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/jquery-jvectormap.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/tests/assets/jquery-jvectormap-world-mill-en.js"></script>
</head>
<body>
  <div id="map" style="width: 600px; height: 400px"></div>
</body>
</html>

顺便说一句,感谢bjornd为伟大的jVectorMap。

© www.soinside.com 2019 - 2024. All rights reserved.