【问题标题】:Jvectormap highlight Multiple countriesJvectormap 突出显示多个国家
【发布时间】:2013-04-19 03:21:00
【问题描述】:

我目前正在使用JvectorMap 并试图在将鼠标悬停在文本上时突出显示多个国家,我已经达到了一个程度,如果我将鼠标悬停在非洲这个词上,它将突出显示整个地图,我将如何过滤它当我将鼠标悬停在非洲的内容名称上时,仅突出显示非洲。

目前我正在使用jQuery.each 创建一个大陆列表,我正在返回continentCodes,其中包含分配给它们的颜色的所有国家代码(ZA、US)...我已经尝试过以下:

jQuery('.continentLink').hover(function() {
 jQuery.each(mapObject.mapData.paths, function(i, val) {
  if (val.continent == "africa"){
   continentCodes[i] = "#3e9d01";
   mapObject.series.regions[0].setValues(continentCodes);
  }
 });
});

但是我重复了每个语句,我无法获得动态大陆。

这是JSFIDDLE

下面是 JS:

jQuery(function(){
//JSON MARKERS
var markers = [{latLng: [-34.033333300000000000, 23.066666700000040000], name: 'Knysna', info:'its got a lake...'},
    {latLng: [-33.924868500000000000, 18.424055299999963000], name: 'Cape Town', info:'its nice...'}];
//JSON MARKERS  

//JSON STYLING
var markerStyle = {initial: {fill: '#F8E23B',stroke: '#383f47'}};
var regionStyling = {initial: {fill: '#128da7'},hover: {fill: "#A0D1DC"}};
//JSON STYLING

//GLOBAL VARIABLES
var countryList = "", continentList = "";
var continentCodes = {};
//GLOBAL VARIABLES

//INIT MAP PLUGIN
jQuery('#world-map').vectorMap({
    map: 'world_mill_en',
    normalizeFunction: 'polynomial',
    markerStyle:markerStyle,
    regionStyle:regionStyling,
    backgroundColor: '#383f47',
    series: {regions: [{values: {},attribute: 'fill'}]},
    markers: markers,
    onRegionClick:function (event, code){
        jQuery('#world-map').vectorMap('set', 'focus', code);
    },
    onMarkerClick: function(events, index){
        jQuery('#infobox').html(markers[index].name);
    }
});
//INIT MAP PLUGIN

var mapObject  = jQuery('#world-map').vectorMap('get', 'mapObject');

//LIST COUNTRIES & CONTINENTS
function createList() {

    //Get list
    jQuery.each(mapObject.mapData.paths, function(i, val) {
        countryList += '<li><a id='+i+' class="countryLink">'+val.name+'</a></li>';
        continentList += '<li><a id='+val.continent+' class="continentLink">'+val.continent+'</a></li>';

        continentCodes[i] = "#3e9d01";
        return continentCodes;
    });
    //display continents
    jQuery('#continentList').html(continentList);

    //display countries
    jQuery('#countryList').html(countryList);

    //Create Hover Function
    jQuery('.continentLink').hover(function() {
        mapObject.series.regions[0].setValues(continentCodes);
        console.log(continentCodes);
    });

    //Create ZOOM Function
    jQuery('.countryLink').click(function(e) {
        jQuery('#world-map').vectorMap('set', 'focus', this.id);
    });
}

createList();
});

和 HTML:

  <div id="world-map" style="width: 960px; height: 400px"></div>
    <div id="infobox"></div>
        <ul id="continentList"></ul>
        <ul id="countryList"></ul>

【问题讨论】:

    标签: javascript jquery each jvectormap


    【解决方案1】:

    我已经对您的代码进行了重组,请参阅JSFIDDLE,这是更正后的 javascript:

    jQuery(function(){
    //JSON MARKERS
    var markers = [{latLng: [-34.033333300000000000, 23.066666700000040000], name: 'Knysna', info:'its got a lake...'},
        {latLng: [-33.924868500000000000, 18.424055299999963000], name: 'Cape Town', info:'its nice...'}];
    //JSON MARKERS  
    
    //JSON STYLING
    var markerStyle = {initial: {fill: '#F8E23B',stroke: '#383f47'}};
    var regionStyling = {initial: {fill: '#128da7'},hover: {fill: "#A0D1DC"}};
    //JSON STYLING
    
    //GLOBAL VARIABLES
    var countryList = "", continentList = "";
    var resultsDup = {};
    var continentCodes = {};
    //GLOBAL VARIABLES
    
    //INIT MAP PLUGIN
    jQuery('#world-map').vectorMap({
        map: 'world_mill_en',
        normalizeFunction: 'polynomial',
        markerStyle:markerStyle,
        regionStyle:regionStyling,
        backgroundColor: '#383f47',
        series: {regions: [{values: {},attribute: 'fill'}]},
        markers: markers,
        onRegionClick:function (event, code){
            jQuery('#world-map').vectorMap('set', 'focus', code);
        },
        onMarkerClick: function(events, index){
            jQuery('#infobox').html(markers[index].name);
        }
    });
    //INIT MAP PLUGIN
    
    var mapObject  = jQuery('#world-map').vectorMap('get', 'mapObject');
    
    //LIST COUNTRIES & CONTINENTS
    jQuery.each(mapObject.mapData.paths, function(i, val) {
    
        countryList += '<li><a id='+i+' class="countryLink">'+val.name+'</a></li>';
    
        //remove duplicate continents
        var resultsList = val.continent;
        if (resultsDup[resultsList]) {
            jQuery(this).remove();
        }else{
            resultsDup[resultsList] = true;
            continentList += '<li><a id='+val.continent+' class="continentLink">'+val.continent+'</a></li>';
        }
        //remove duplicate continents
    
    
    });
    //display countries
    jQuery('#countryList').html(countryList);
    
    //display continents
    jQuery('#continentList').html(continentList);
    
    var continentId =""
    function getID(continentId){
        jQuery.each(mapObject.mapData.paths, function(i, val) {
                if (val.continent == continentId){
                    continentCodes[i] = "#3e9d01";
                    mapObject.series.regions[0].setValues(continentCodes);
                }
        });
    }
    
    function removeGetID(continentId){
        jQuery.each(mapObject.mapData.paths, function(i, val) {
                if (val.continent == continentId){
                    continentCodes[i] = "#128da7";
                    mapObject.series.regions[0].setValues(continentCodes);
                }
        });
    }
    
    //LIST COUNTRIES & CONTINENTS TEMP
    jQuery('.continentLink').hover(function(e) {
        continentId = this.id;
        getID(continentId);
    }, function(){
        removeGetID(continentId);
    });
    
    //Zoom to Country Function
    jQuery('.countryLink').click(function(e) {
        jQuery('#world-map').vectorMap('set', 'focus', this.id);
    });
    
    //Continent Hover function
    
    });
    

    享受:D

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多