How to use jVectorMap to get state name from array of maps inside onRegionClick callback?

I am using the jVectorMap plugin and I am trying to get the name element from a map array that looks like this:

$.fn.vectorMap('addMap', 'usa_en', {
    "width": 959,
    "height": 593,
    "pathes": {
        "hi": {
            "path": "COORDINATES_GO_HERE",
            "name": "Hawaii"
        },
        "ak": {
            "path": "COORDINATES_GO_HERE",
            "name": "Alaska"
        },
        "fl": {
            "path": "COORDINATES_GO_HERE",
            "name": "Florida"
        },
        ...and so on for the other 47 states
    }
})

The plugin is launched using the following, and the map array file is determined by the "map" setting:

var myData = {"hi":0,"ak":0,"fl":0, ...and so on}

$('#us-map').vectorMap({
    map: 'usa_en',
    values: myData,
    color: '#ccc',
    onRegionClick: function(event, code){
        $.get('{site_url}embeds/state_view/'+code, function(data) {
            $('#data-replace').fadeOut(200,function(){ $(this).html(data).fadeTo(200,1); });
            $('#data-title').fadeOut(200,function(){ $(this).text(INSERT_CLICKED_STATE_NAME_HERE).fadeTo(200,1); });
        });
    }
});

Any ideas on how I can insert a state name from an array of map files into an onRegionClick callback?

+5
source share
1 answer

You need to get a handle to the map object, then you can use the method getRegionName():

onRegionClick: function(event, code){
    //obtain the reference to the map object
    var map = $('#world-map').vectorMap('get', 'mapObject');

    $.get('{site_url}embeds/state_view/'+code, function(data) {
        $('#data-replace').fadeOut(200,function(){ $(this).html(data).fadeTo(200,1); });
        $('#data-title').fadeOut(200, function(){ $(this).text(map.getRegionName(code)).fadeTo(200,1); });
    });
+5
source

All Articles