MapSVG Map Builder

Since February 2016 MapSVG jQuery plugin turned into a standalone HTML/JS app MapSVG Builder which can be easily integrated into any project. In most cases you won't need to do any coding, so all you need is common MapSVG Builder documentation.

If you still want to dive into the code, continue reading.

Installation

Copy all files to your server. Include files into your page (your paths may be different):

<link href="/css/mapsvg.css" rel="stylesheet">
<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript" src="/js/jquery.mousewheel.js"></script>
<script type="text/javascript" src="/js/mapsvg.min.js"></script>

Updating to jQuery MapSVG Builder 2.0 from previous versions

1. Starting from jQuery MapSVG Builder 2.0 there's no more need in raphael.js library.
2. Now there's mapsvg.css file. Don't edit it or you will lose your changes with the next plugin update. Instead create a child css file like mapsvg-custom.css and insert it after main mapsvg.css file
3. Some parameter names were changed. For example, tooltipsMode parameter was changed to tooltips: {mode: "..."}. Please read documentation and update your code.

Create a container for your map

<div id="mapsvg"></div>

Call MapSVG plugin

$('#mapsvg').mapSvg({source: '/maps/usa.svg'});

Options

When you call MapSVG plugin you can pass it an object with following options:

Name type default description
source string URL of SVG file. Must be located on the same domain as your website.
colors object
{
background: "#eee",
selected: 40,
hover: 20
}
This parameter overrides colors defined in SVG file.
Colors can be defined in any CSS-compatible format (hex / rgb / rgba).
Also you can use the word transparent or image URL url(path/to/img.png)

background - background color
base - base color. Overrides default SVG colors of all regions. Leave blank if you want to keep colors from SVG file. But if a region has it's own color setting in MapSVG, that settings has more priority than "base" color.
selected - color of selected state, or relative brightness change from default color. For example, 20 means "increase brightness by 20", -10 means "darken color by 10"
hover - color of hover state. Same rules as for selected color.
disabled - color of disabled regions. Leave blank if you don't want to change color of regions when they are disabled.
strokes - color of strokes
width float as defined in SVG file width of a map, ignored if responsive parameter is ON
height float as defined in SVG file height of a map, ignored if responsive parameter is ON
viewBox array as defined in SVG file parameter defining initial position and zoom level: [topX, topY, width, height]. Could be a hard thing to undestand so you really need to read SVG documentation on this.
tooltips object
{mode: "off"}
Global tooltips.

Region / Marker local tooltips has priority over global tooltips.
{mode: "off"} - no global tooltips

{mode: "id"} - show region IDs (taken from SVG file for Regions, or from MapSVG settings for Markers)

{mode: "title"} - show Region titles (taken from SVG file)

{mode: function(region){ ... }} - custom function for a tooltips, should return a string.

Example:
{mode: function(region){
    return '<b>' + region.id + '</b>' + region.title }
}
popovers object
{mode: "off"}
Global popovers.

Region / Marker local popovers has priority over global popovers.
{mode: "off"} - no global popovers
{mode: function(region){ ... }} - custom function for global popovers, should return a string.

Example:
{mode: function(region){
    return '<b>' + region.id + '</b>' + region.title }
}
zoom object
{
  on: false,
  buttons: {position: "right"},
  limit: [0,10]
}
Map zooming.
on: false | true
buttons.position: "right" | "left" | "off"
limit: [MIN,MAX] - minimum and maximum zoom levels starting from initial map position.
scroll object
{on: false}
Map scrolling.
on: false | true
responsive boolean true Responsiveness, true | false
multiSelect boolean false Select multiple regions, true | false
cursor string "default" Set cursor style on mouse hover: "default" | "pointer"
loadingText string Loading map... Text shown during map loading.
gauge object
{on: false,
labels: {
  low: "low",
  high: "high"
},
colors: {
  low: "#550000",
  high: "#ee0000"}
}
Lets you to represent statistical information by every region of the map with different shades of color: number of visitors from each country, population, income, etc.

To get started, try to set {on:true} and then set gaugeValue for a few Regions.
menu object
{on: false,
  containerId: "",
  template: function(region){
  return '<li><a href="' + region.id + '">' + (region.title||region.id) + '</a></li>'
}}
Adds menu with clickable list of Regions into a container with given containerId.

If you want a menu to be created automatically - just create an empty <ul id="..."></ul> container.

If you want to change order of menu items or do any other manipualtions - fill the container manually with menu items:
<ul id="...">
  <li><a href="REGION-ID">ANYTHING</a></li>
  <li><a href="REGION-ID">ANYTHING</a></li>
  ...
</ul>
onClick function Event handler. Example:
function(e,mapsvg){
  var region = this;
  alert(this.id);
}
Parameters:
this - MapObject (could be a Region or a Marker)
e - event object (same as in jQuery)
mapsvg - MapSVG instance
mouseOver function Event handler. Example:
function(e,mapsvg){
  var region = this;
  alert(this.id);
}
Parameters:
this - MapObject (could be a Region or a Marker)
e - event object (same as in jQuery)
mapsvg - MapSVG instance
mouseOut function Event handler. Example:
function(e,mapsvg){
  var region = this;
  alert(this.id);
}
Parameters:
this - MapObject (could be a Region or a Marker)
e - event object (same as in jQuery)
mapsvg - MapSVG instance
beforeLoad function Event handler. Called before MapSVG starts rendering SVG file. Example:
function(){
  var mapsvg = this;
  mapsvg.setZoom({on: true});
}
Parameters:
this - MapSVG instance
afterLoad function Event handler. Called after MapSVG finished rendering SVG file. Example:
function(){
  var mapsvg = this;
  mapsvg.setZoom({on: true});
}
Parameters:
this - MapSVG instance
regions object Settings for Regions. Explained below.
markers array Settings for Markers. Explained below.

Regions

SVG file may have many shapes but only those with color fill become clickable active Regions. Each Region must have a proper ID in SVG file:

<path id="DE" title="Germany" d="M 100,200 300,400 ... " />

IDs in SVG files have some restrictions - they can't contain spaces, they can't start with a number. Sometimes InkScape vector editor does't play well with non-latin characters in IDs either. So that's why it's a good idea to have title parameter as well.

So if you want to change settings for some of your Regions, you should pass a regions object into MapSVG in following format:

$('#mapsvg').mapSvg({
  source: 'world.svg',
  regions: {'Germany': { OPTIONS }, 'Sweden': { OPTIONS } }
})

Region options

name type default description
disabled boolean false Disable a region of you don't want it to be clickable.
fill string as defeined in SVG file Color of a Region. You can use color in hex / rgb / rgba format.
tooltip string Tooltip, shown on hover. If defined, overrides global tooltips.
popover string Popover info window, shown on hover. Stays on until closed. If defined, has overrides global popovers.
href string URL link to any resource.
target string Link target. Set as blank if you want a link to open in new tab.
data object Custom Data object which can contain anything. Accessible via Region.data. For example, you can use it to store some parameters for later use in event handlers:
{myParameter: "Hello World!"}
gaugeValue integer Number value representing some statistical information which has effect on Region color if gauge parameter is turned on.

Example

$('#mapsvg').mapSvg({regions:
  {
    'USA': {
             tooltip: 'USA: Click to go to Google.com',
             fill: '#ff0000',
             href: 'http://google.com'
            },
    'France': {tooltip: 'This is France!'}
  }
);

Markers

There are two ways to set markers:
1. By X/Y coordinates (works on any map). Keep in mind that X/Y coordinates are relative to SVG coordinate system;
2. By latitude / longitude coordinates (works only on geo-calibrated maps located in /maps/geo-calibrated folder ).

You can get latitude/longitude coordinates of any object from http://maps.google.com: Move the mouse to needed place, right-click and choose 'What's Here?'.

There are some built-in marker images in /markers folder, but you can add your own. Keep in mind that markers are positioned by a center point located at marker's bottom.

Marker options

Name type default description
id string marker_XXX Unique ID. If you don't set an ID, it will be generated automatically with incrementing number: marker_0, marker_1, ...
src string {PLUGIN_URL} / markers / pin1_red.png Image URL.
geoCoords array Latitude/longitude coordinates. Works only on geo-calibrated maps. Example for London: [51.49763,-0.148315]
tooltip string Tooltip, shown on hover. If defined, overrides global tooltips.
popover string Popover info window, shown on hover. Stays on until closed. If defined, has overrides global popovers.
href string URL link to any resource.
target string Link target. Set as blank if you want a link to open in new tab.
data object Custom Data object which can contain anything. Accessible via Region.data. For example, you can use it to store some parameters for later use in event handlers:
{myParameter: "Hello World!"}

Example

$('#mapsvg').mapSvg({markers: [
     {
       src:  '/markers/pin1_red.png'
       geoCoords: [57.162499, 65.54718],
       tooltip: 'Click to go to http://yahoo.com',
       href: 'http://yahoo.com'
     },
     {
       id: 'Washington_DC',
       geoCoords: [38.893438,-77.03167],
       tooltip: 'Washington, DC'
     }
]});

Final example

$('#mapsvg').mapSvg({
    source: '/maps/geo-calibrated/world-low-resolution.svg',
    colors: {
               base: "#E1F1F1",
               background: "#eeeeee",
               hover: 15,
               selected: 30,
               disabled: "#ffffff",
               stroke: "#7eadc0"
            },
    regions : {
                'USA':  {tooltip: 'USA: Click to go to Google.com', href: 'http://google.com'},
                'Brazil': {tooltip: ' Brazil is the largest country in South America.'}
               },
    markers: [
               { geoCoords: [38.893438,-77.03167],
                 tooltip: 'Washington, DC',
               },
               { geCoords: [51.49763,-0.148315],
                 tooltip: 'London',
               }
              ],
    tooltips: {mode: "title"},
    zoom: {on: true},
    scroll: true,
    responsive: true
});

Styling

Look into mapsvg.css file to find classes used by MapSVG. Don't edit that file or you will lose your changes with the next plugin update. Instead create a child css file like mapsvg-custom.css and insert it after the main mapsvg.css file. Most often use for custom CSS file is styling of popovers and tooltips.