Documentation
Your download includes a working example as well as all required files in the /dist directory, this documentation refers to files in that directory.
Installation
Follow these steps to add the JSMaps plugin to your site.
Files
Start by including the jsmaps and maps folders in the root of your project i.e. the same directory as the html file that will hold your map.
Map scripts
Include the following css file in the head section of your page:
<link href="jsmaps/jsmaps.css" rel="stylesheet" type="text/css" />
Jquery is required for this map to work, if jquery is already included in your page you don't need to include it again. If not, include the following script before the closing </body> tag of your page:
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
Include the following javascript tags before the closing </body> tag of your page (after jQuery):
<script src="jsmaps/jsmaps-libs.js" type="text/javascript"></script>
<script src="jsmaps/jsmaps-panzoom.js" type="text/javascript"></script>
<script src="jsmaps/jsmaps.min.js" type="text/javascript"></script></script>
Include the JSMaps plugin javascript files, make sure to update the map to the one you want to use if it's not the world map:
//Use any map as named in the maps folder e.g. maps/usa.js
<script src="maps/world.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$('#world-map').JSMaps({
map: 'world' //Use any map as named in the maps folder e.g. usa
});
});
</script>
Note: jsmaps-panzoom.js is used by the pan/zoom functionality, if you don’t use this option, you don’t have to load this file.
Note: maps/world.js is the location of the map data, it can be any map and the location is up to you, but it must be loaded last.
Map markup
Add the following HTML in the body tag of your page where you want to include the map. Note that the map size will be defined by its parent container size:
<div class="jsmaps-wrapper" id="world-map"></div>
Note: the jsmaps-wrapper class is required, you may choose any id when initiating the map plugin e.g. $('#my-id').JSMaps({...});
Settings
Global properties
Here is a list of the global settings and their default values, you can override these when initiating the plugin, see below the list for an example:
Name | Type | Default | Description |
---|---|---|---|
map* | string | The name of the map, must match a map from the maps folder |
|
mapWidth / mapHeight* | number | These are the original svg dimensions and are there purely so the plugin knows the map ratio when resizing the map. Do not change these values. The map will always fill the space available in the container you put it in, so to change the size of the map, change the size of its container. |
|
responsive | bool | true | If true , will trigger a window resize listener |
offColor | hex | #ccc | Color when a state is disabled |
strokeColor | hex | #24221f | Stroke color for states |
offStrokeColor | hex | #444 | Stroke color for disabled states |
strokeWidth | number | 1 | Size of te stroke in pixels |
abbreviationColor | hex | #f2f2f2 | Text color of state abbreviations |
abbreviationFontSize | number | 12 | Font size of state abbreviations in pixels |
displayAbbreviations | bool | true | If true , will display abbreviations on states |
displayAbbreviationOnDisabledStates | bool | false | If true , will display abbreviations on disabled states |
autoPositionAbbreviations | bool | false | Position abbreviations automatically using Raphael getBBox. If true textX/textY state properties become offset values |
stateClickAction | 'text' | 'url' | 'none' |
'text' | Action to take when state is clicked |
textPosition | 'left' | 'right' | 'bottom' |
'right' | Position of textarea if stateClickAction is set to text |
hrefTarget | '_self' | '_blank' | '_parent' | '_top' |
'_blank' | href target when state is clicked if using urls |
textAreaWidth | number | 300 | Width of text area if to the left or right of the map, if bottom textarea will be width of map |
textAreaHeight | number | 300 | Height of textarea if at bottom, if to the left or right height will be height of the map |
pinSize | number | 10 | Size of pins on the map in pixels |
displayMousePosition | bool | false | If true , displays current mouse position on screen to help position pins |
displayViewBox | bool | false | If true , displays current map position and zoom level, this is handy to figure out initialZoom/initialMapX/initialMapY |
enablePanZoom | bool | false | Enable pan/zoom functionality |
initialZoom | number | 1 | The initial zoom level of the map, default is 1 which is the minimum, any number greater will zoom deeper |
initialMapX | number | 0 | The initial part of the map to be displayed in the center X of the view if initialZoom is greater than 1, to obtain this number, set the displayViewBox and using the pan/zoom functionality, set the map to your desired initial position and zoom level, the initialMapX/initialMapY/Zoom values will be displayed in the top right corner of the map |
initialMapY | number | 0 | The initial part of the map to be displayed in the center Y of the view if initialZoom is greater than 1, to obtain this number, set the displayViewBox and using the pan/zoom functionality, set the map to your desired initial position and zoom level, the initialMapX/initialMapY/Zoom values will be displayed in the top right corner of the map |
retainPanZoomOnRedraw | bool | true | If true , the map position and zoom level will be retained when using the reDraw event |
displayPreloader | bool | true | If true , display a preloader while the map renders. |
preloaderText | string | 'Loading map...' | Text to display in the preloader, set this to an empty string if you want to use a css spinner instead. |
disableTooltip | bool | false | If true , disable tooltip on hover |
groups | Array<string> | [] | List of groups of state/regions, see our tutorial for more information. |
selectElement | bool | true | Display a select element above the map, including all regions/states, groups and pins. |
selectElementDevices | Array<string> | ['mobile'] | Filter which device to display the select element on: ['mobile', 'tablet', 'desktop'] |
selectElementDefaultText | string | 'Please select' | Text value of the default select option |
Individual state/region/pin settings
All the state/region/pin settings for the map are located in maps/{mapName}.js and need to be altered in that file:
Name | Type | Default | Description |
---|---|---|---|
name | string | Text displayed in tooltip, supports html |
|
abbreviation | string | Abbreviation (if available) displayed on the state/region/pin |
|
textX/textY | number | 0 | Abbreviation X/Y position if set manually, set the global displayMousePosition option to true, mouse over the map and use the X/Y value displayed in the top left of the screen. Values are included in some maps, you can also set autoPositionAbbreviations to true in global settings. |
color | hex | Default state/region/pin background color |
|
hoverColor | hex | State/region/pin hover background color |
|
selectedColor | hex | State/region/pin selected background color |
|
url | string | 'https://jsmaps.io' | URL to redirect to when State/region/pin is clicked (if global stateClickAction = url) |
text | string | Text displayed in textarea when State/region/pin is clicked (if global stateClickAction = text) |
|
src | string | Path to image to be used as a pin instead of a circle, see our tutorial for more information. |
|
srcWidth | number | The original width of the image to be used as a pin instead of a circle, see our tutorial for more information. |
|
srcHeight | number | The original height of the image to be used as a pin instead of a circle, see our tutorial for more information. |
|
pinWidth | number | The size of the image pin on the map, see our tutorial for more information. |
Events
The map includes custom events which you can trigger externally:
Name | Type | Default | Description |
---|---|---|---|
reDraw | func | Trigger a map redraw, see our tutorial for more information. |
|
stateClick | func | Trigger click events on state/region/pin/group |
|
stateUnClick | func | Trigger unclick events on state/region/pin/group |
Callbacks
The map will dispatch event callbacks so you can add custom interactivity to your project, see below for the full list.
Name | Type | Default | Description |
---|---|---|---|
onReady | func | The map is fully rendered and ready for interactions |
|
onStateClick | func | A state/region has been clicked, data is passed to the listener which includes all data defined for the state/region/pin in maps/{mapName}.js |
|
onStateOver | func | The mouse enters a state, data is passed to the listener which includes all data defined for the state/region/pin in maps/{mapName}.js |
|
onStateOut | func | The mouse leaves a state, data is passed to the listener which includes all data defined for the state/region/pin in maps/{mapName}.js |