1

I am using JqvMap and I want to click on a region and this shall prompt a (bootstrap) popover with the name of the country as title, and the content should be some html links. This is my code:

<script type="text/javascript">
jQuery(document).ready(function() {
        jQuery('#vmap').vectorMap({
        map: 'world_en',
        backgroundColor: '#333333',
        color: '#ffffff',
        hoverOpacity: 0.7,
        selectedColor: '#666666',
        enableZoom: true,
        showTooltip: false,
        values: sample_data,
        scaleColors: ['#C8EEFF', '#006491'],
        normalizeFunction: 'polynomial',
        regionsSelectableOne: 'true',
        onRegionClick: function(element, code, region) {
            $(".popover-title").html(region);
            jQuery('.jvectormap-region').popover({
                placement: 'top',
                container: '#vmap',
                content: '<a href="#">page 1</a></br><a href="#">page 2</a></br><a href="#">page 3</a></br><a href="#">page 4</a></br>',
                trigger: 'click',
                html: 'true',
                title: ' '
            });
        },
        onRegionOver: function (event, code, region) {
            document.body.style.cursor = "pointer";
        },
        onRegionOut: function (element, code, region) {
            document.body.style.cursor = "default";
            $('.jvectormap-region').popover('destroy');
            // $('#vmap').vectorMap('deselect', code);
        }
    });
});
</script>

My problem at the moment is that I need to click twice on the map to make popover show up. I read it may be due to the fact that it is not initialized, but I can't seem to initialize it (where? how?)!

Can someone help me with these issues? I can't seem to figure out what the problem is..

Manuel Maestrini
  • 409
  • 6
  • 15

2 Answers2

0

So I kinda fixed it (probably in a nasty way but it does the trick). Hope it will help someone else.

<script type="text/javascript">
jQuery(document).ready(function() {
    jQuery('#vmap').vectorMap({
        map: 'world_en',
        backgroundColor: '#333333',
        color: '#ffffff',
        hoverOpacity: 0.7,
        selectedColor: '#666666',
        enableZoom: true,
        showTooltip: false,
        values: sample_data,
        scaleColors: ['#C8EEFF', '#006491'],
        normalizeFunction: 'polynomial',
        regionsSelectableOne: true,
    });
    runPopOver();
});
</script>

<script type="text/javascript">
function runPopOver() {
    var currentRegion;
    jQuery('#vmap').bind('regionMouseOver.jqvmap',
        function(event, code, region) {
            document.body.style.cursor = "pointer";
            currentRegion = region;
        }
    );
    jQuery('#vmap').bind('regionMouseOut.jqvmap',
        function(event, code, region) {
            document.body.style.cursor = "default";
        }
    );
    jQuery('#vmap').bind('regionClick.jqvmap',
        function(event, code, region) {
            if ($('#vmap [id^="popover"]').length > 1) {
                $('#vmap [id^="popover"]').first().remove();
            }
            var snapshot_url = "http://www.business-anti-corruption.com/country-profiles/europe-central-asia/" + region + "/snapshot.aspx";

            $(".popover-title").html(region);
            $(".popover-content").html('<a href=' + snapshot_url + ' target="_blank">Snapshot</a></br><a href="#">page 2</a></br><a href="#">page 3</a></br><a href="#">page 4</a></br>');
        }
    );

    jQuery('.jvectormap-region').popover({
        placement: 'left',
        container: '#vmap',
        html: 'true',
        title: ' '
    });
}
</script>

So basically when I click on a region if there is more than 1 popover (even to be opened) I get a list of all the popover (2), take the first and remove it from the DOM (.first().remove()).

Manuel Maestrini
  • 409
  • 6
  • 15
0

Here's a more permanent fix:

in the jquery.vmap.js find this bit of code:

  jQuery(params.container).delegate(this.canvas.mode == 'svg' ? 'path' : 'shape', 'click', function (e) {
  if (!params.multiSelectRegion) {
    for (var key in mapData.pathes) {
      map.countries[key].currentFillColor = map.countries[key].getOriginalFill();
      map.countries[key].setFill(map.countries[key].getOriginalFill());
    }
  }

  var path = e.target;
  var code = e.target.id.split('_').pop();

  jQuery(params.container).trigger('regionClick.jqvmap', [code, mapData.pathes[code].name]);
  if (!regionClickEvent.isDefaultPrevented()) {
    if (map.selectedRegions.indexOf(code) !== -1) {
      map.deselect(code, path);
    } else {
      map.select(code, path);
    }
  }

  //console.log(selectedRegions);

});

Replace it with this:

        jQuery(params.container).delegate(this.canvas.mode == 'svg' ? 'path' : 'shape', 'mousedown mouseup', function (e) {

            var PageCoords;
            if (e.type == 'mousedown') {
                pageCoords = event.pageX + "." + event.pageY;
            }
            if (e.type == 'mouseup') {
                var pageCoords2 = event.pageX + "." + event.pageY;

                if (pageCoords == pageCoords2) {

                    //we have a click. Do the ClickEvent
                    if (!params.multiSelectRegion) {
                        for (var key in mapData.pathes) {
                            map.countries[key].currentFillColor = map.countries[key].getOriginalFill();
                            map.countries[key].setFill(map.countries[key].getOriginalFill());
                        }
                    }

                    var path = e.target;
                    var code = e.target.id.split('_').pop();

                    regionClickEvent = $.Event('regionClick.jqvmap');

                    jQuery(params.container).trigger('regionClick.jqvmap', [code, mapData.pathes[code].name]);

                    if (!regionClickEvent.isDefaultPrevented()) {
                        if (map.selectedRegions.indexOf(code) !== -1) {
                            map.deselect(code, path);
                        } else {
                            map.select(code, path);
                        }
                    }
                } 

            }

Instead of triggering a click immediately, the script now checks if it's a click or a drag. If it's a click, it fires the code you put in our onRegionClick.