Add an interactive trail map to your blog
This article will show you how to create an interactive world map page in your GitHub Pages blog based on Hexo and Next. This map will display the cities you have visited. You can display different colored markers on the map based on the frequency of your visits to each city. You can also click on these markers to display more information about the city.
Preparation
You need to install the following tools:
- Node.js and NPM
- Hexo
Make sure your blog has been deployed to GitHub Pages and that your local development environment has been set up correctly.
Step 1: Create a new page
In the root directory of your Hexo project, run the following command:
1 | hexo new page "travel" |
This command will be insourceCreate a folder named "travel" in the directory and create aindex.mdfile.
Step 2: Install Leaflet
In the root directory of your Hexo project, run the following command:
1 | npm install leaflet |
then inindex.mdThe top of the file introduces Leaflet’s CSS and JS:
1 | --- |
Step 3: Create a map container
inindex.md, create a map to host the<div>element and then set an explicit height to it via CSS:
1 | <style> |
Step 4: Initialize the map
Initialize the map using Leaflet's API and set its center to longitude 0, latitude 0 (mid-Atlantic), and set the initial zoom level to 2.
1 | <script> |
Step 5: Add city data
Create a GeoJSON file to store city data, including the city's name, latitude and longitude, the number of times you have visited, and a URL to an image. The format of the file should be as follows:
1 | { |
Save this file in your Hexo projectsourcedirectory, for example, you could name itcities.json。
Step 6: Load the data and display it on the map
inindex.mdfile, add the following code to load the GeoJSON file and then display the data on the map:
1 | <script> |
The above JavaScript code will run after the page is loaded. It first initializes the map, then loads and parses the GeoJSON file. For each city, a circular marker is created, with the color determined based on the number of visits. When the marker is clicked, a window pops up with the city name and picture.
At this point, your blog has successfully added an interactive footprint map page.