在jVectorMap中,如果满足条件,如何选择区域?

时间:2015-04-21 11:26:35

标签: javascript jquery jvectormap

我正在使用jVectorMaps。默认情况下,它不允许选择区域UNLESS>>> regionsSelected: true

这是我想要实现的目标:

当用户点击某个区域时,系统会提示他/她从加载的JSON文件中提取问题。如果问题的答案是正确的,我需要区域颜色才能更改

到目前为止我的代码:

$(function(){
  map = new jvm.Map({
    container: $('#map'),
    map: 'world_mill_en',//map file
    backgroundColor: ['#1E90FF'],//map background color    
    regionsSelectable:true,//this needs to be true to be able to select a region

    onRegionClick: function(event, code){
      var map = $('#map').vectorMap('get', 'mapObject');
      var regionName = map.getRegionName(code);
      $.getJSON('../countries.json', function(data) {
        for (var i in data.country) {
          if (data.country[i].name === regionName){
            var answer = prompt(data.country[i].question, "Type your answer here.");
            if(data.country[i].answer === answer){
              alert("Correct!");
            }else{
              alert("Wrong. Try again.");
            }
          }
        }
      });
    },

  });
});

我想我必须找到一种方法,在满足regionsSelectable条件后动态更改if(data.country[i].answer === answer)的属性值。如果不是,那么regionsSelectable的属性值应保持为false

我是一名初学者,所以任何批评都非常受欢迎,无论如何,我可能已经离开这里了! :)

1 个答案:

答案 0 :(得分:0)

试试这个..

 $(function(){
          map = new jvm.Map({
            container: $('#map'),
            map: 'world_mill_en',//map file
            backgroundColor: ['#1E90FF'],//map background color    
            regionsSelectable:true,//this needs to be true to be able to select a region

            onRegionClick: function(event, code){
              var map = $('#map').vectorMap('get', 'mapObject');
              var regionName = map.getRegionName(code);
              $.getJSON('../countries.json', function(data) {
                for (var i in data.country) {
                  if (data.country[i].name === regionName){
                    var answer = prompt(data.country[i].question, "Type your answer here.");
                    if(data.country[i].answer === answer){
                      map.setSelectedRegions(code);;
                    }else{
                     map.clearSelectedRegions();

                    }
                  }
                }
              });
            },

          });
        });