A map of China is used in a recent data statistics project, which is to dynamically display the statistical data of a certain province and region in a certain time period on the map. We don’t need flash, we just rely on raphael.js and SVG images. Complete the interactive operation of the map. In this article, I will share with you how to use js to complete map interaction.
Let’s briefly introduce raphael.js. raphael.js is a small javascript library that can draw various vector graphics, various charts, image cropping, rotation, motion animation and other functions on web pages. In addition, raphael.js is also cross-browser compatible, and it is also compatible with the old IE6. The official website address of raphael.js: http://raphaeljs.com/
Preparation
We need to prepare a vector map. You can find a vector map from the Internet, or use illustrator to draw a vector map, and then export it to a file in SVG format. This file can be opened on a browser and then the content inside is extracted. path path information (coordinates starting with M). And prepare the map path information according to the format of chimamapPath.js.
The above is how we encapsulate the prepared map path information into the () function, and save the file name as chinamapPath.js for later calling.
HTML
First load the raphael.js library file and chimamapPath.js path information file in the head section.
Then place div#map in the body where the map needs to be placed.
JAVASCRIPT
First we call the map in the page, the method is as follows:
At this time, when we open it with a browser, the loaded map will be displayed. Next, we need to add province names to the corresponding province areas in the map, and a color-changing animation effect when the mouse slides over each province block.