Marek123
Marek123

Reputation: 1211

jvectormap - define selectable regions

is it possible in jVectorMaps to define static regions that can be selected?
I need to define only 6 regions that a user is allowed to select.
The tricky part would be, that I need to have Europe, Asia and World as a region and "Poland" and "Canada".

If a user selects poland, it should select just "Poland" but if a user selects any other country in "Europe", it should selected all european countries.

Is this possible with jvectormaps?

Upvotes: 0

Views: 1839

Answers (1)

deblocker
deblocker

Reputation: 7687

jVectorMap regions are SVG paths identified by the 2-letters ISO Country codes.

You may not merge that paths, but you can collect that country codes into macro-areas and use this groups of codes to select all the jVectorMap regions you need at once.

This is an example with 4 macro-areas: Poland, Canada, Europe and the rest of the world.

$(document).ready(function () {
  // Group states into Areas
  var areas = [];
  areas[0] = [];
  areas[1] = ["PL"];
  areas[2] = ["BE","FR","BG","DK","HR","DE","BA","HU","FI","BY","GR","NL","PT","NO","LV","LT","LU","XK","CH","EE","IS","AL","IT","CZ","GB","IE","ES","ME","MD","RO","RS","MK","SK","SI","UA","SE","AT"];
  areas[3] = ["CA"];

  function selectArea(code){
    var mapObj = $("#map").vectorMap("get", "mapObject");
    areas.forEach(function(area) {
      if(area.indexOf(code)>-1) {
        mapObj.setSelectedRegions(area);
        return;
      }
    });
  }

  function clearAll(){
    var mapObj = $("#map").vectorMap("get", "mapObject");
    mapObj.clearSelectedRegions();
  }

  $("#map").vectorMap({
    map: "world_mill",
    backgroundColor: "aliceblue",
    zoomOnScroll: true,
    regionsSelectable: true,
    regionStyle: {
      initial: {
        fill: "lightgrey"
      },
      selected: {
        fill: "darkseagreen"
      }
    },
    onRegionClick: function(e, code){
      clearAll();
      selectArea(code);
      return false;
    }
  });

  (function () {
    // Collect the rest of the World
    var mapObj = $("#map").vectorMap("get", "mapObject");
    var states = areas.join(",");
    for(var code in mapObj.regions) {
      if(mapObj.regions.hasOwnProperty(code)) {
        if(states.indexOf(code) == -1) {
          areas[0].push(code);
        }
      }
    }
  })();

});
<html>
<head>
  <title>jVectorMap Areas</title>
  <link rel="stylesheet" href="http://jvectormap.com/css/jquery-jvectormap-2.0.3.css" type="text/css">
  <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
  <script src="http://jvectormap.com/js/jquery-jvectormap-2.0.3.min.js"></script>
  <script src="http://jvectormap.com/js/jquery-jvectormap-world-mill.js"></script>
</head>
<body>
  <div id="map" style="width: 600px; height: 400px"></div>
</body>
</html>

Upvotes: 2

Related Questions