├── .gitignore ├── CHANGELOG.md ├── rollup.config.mjs ├── package.json ├── src └── index.js ├── example ├── static-html │ └── index.html └── common │ └── map-with-markers.js ├── dist └── index.js ├── index.html └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # 0.0.2 2 | 3 | - Explicitly import `mapboxgl` for use in react app 4 | 5 | # 0.0.1 6 | 7 | - First -------------------------------------------------------------------------------- /rollup.config.mjs: -------------------------------------------------------------------------------- 1 | export default { 2 | input: 'src/index.js', 3 | output: { 4 | name: 'FontawesomeMarker', 5 | file: 'dist/index.js', 6 | format: 'umd', 7 | globals: { 8 | 'mapbox-gl': 'mapboxgl' 9 | } 10 | } 11 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mapbox-gl-fontawesome-markers", 3 | "version": "0.0.2", 4 | "description": "Adds Fontawesome icons to Mapbox GL JS default Map Markers.", 5 | "main": "dist/index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "build": "rollup -c" 9 | }, 10 | "author": "", 11 | "license": "ISC", 12 | "devDependencies": { 13 | "rollup": "^3.9.1" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import mapboxgl from 'mapbox-gl'; 2 | 3 | const DEFAULT_TOP_OFFSET = 7 4 | const DEFAULT_FONT_SIZE = 12 5 | 6 | class FontawesomeMarker extends mapboxgl.Marker { 7 | constructor(options, legacyOptions) { 8 | super(options, legacyOptions); 9 | 10 | const { 11 | icon, 12 | iconColor = '#FFF', 13 | scale = 1 14 | } = options 15 | 16 | if (icon) { 17 | const top = scale * DEFAULT_TOP_OFFSET 18 | const fontSize = `${DEFAULT_FONT_SIZE * scale}px` 19 | const iconElementString = `` 20 | this._element.insertAdjacentHTML('beforeend', iconElementString) 21 | 22 | // remove the default circle from the marker 23 | this._element.querySelector('circle').remove() 24 | } 25 | } 26 | } 27 | 28 | export default FontawesomeMarker -------------------------------------------------------------------------------- /example/static-html/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Display a globe on a webpage 7 | 8 | 9 | 10 | 11 | 12 | 25 | 26 | 27 | 28 |
29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /dist/index.js: -------------------------------------------------------------------------------- 1 | (function (global, factory) { 2 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('mapbox-gl')) : 3 | typeof define === 'function' && define.amd ? define(['mapbox-gl'], factory) : 4 | (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.FontawesomeMarker = factory(global.mapboxgl)); 5 | })(this, (function (mapboxgl) { 'use strict'; 6 | 7 | const DEFAULT_TOP_OFFSET = 7; 8 | const DEFAULT_FONT_SIZE = 12; 9 | 10 | class FontawesomeMarker extends mapboxgl.Marker { 11 | constructor(options, legacyOptions) { 12 | super(options, legacyOptions); 13 | 14 | const { 15 | icon, 16 | iconColor = '#FFF', 17 | scale = 1 18 | } = options; 19 | 20 | if (icon) { 21 | const top = scale * DEFAULT_TOP_OFFSET; 22 | const fontSize = `${DEFAULT_FONT_SIZE * scale}px`; 23 | const iconElementString = ``; 24 | this._element.insertAdjacentHTML('beforeend', iconElementString); 25 | 26 | // remove the default circle from the marker 27 | this._element.querySelector('circle').remove(); 28 | } 29 | } 30 | } 31 | 32 | return FontawesomeMarker; 33 | 34 | })); 35 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Mapbox GL Fontawesome Markers Demo 7 | 8 | 9 | 10 | 11 | 12 | 38 | 39 | 40 | 41 |
42 |
43 |
Mapbox GL Fontawesome Markers Demo
44 |
Github Repo
45 | Reload to regenerate markers 46 |
47 | 48 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mapbox-gl-fontawesome-markers 2 | 3 | Adds Fontawesome icons to Mapbox GL JS default Map Markers. 4 | 5 | This package extends `mapboxgl.Marker`, adding two new options: `icon` and `iconColor`. `icon` is any fontawesome icon class (e.g. `fa-solid fa-pizza-slice`) and `iconColor` is any html or hex color code. The fontawesome icon specified in `icon` will be added to the marker in place of the default white circle. 6 | 7 | All built-in [`mapboxgl.Marker`](https://docs.mapbox.com/mapbox-gl-js/api/markers/#marker) options are still available, so you can still set the `color`, `scale`, `rotation`, etc as usual. 8 | 9 | [Live Demo on Github Pages](https://chriswhong.github.io/mapbox-gl-fontawesome-markers/) 10 | 11 | Mapbox_GL_Fontawesome_Markers_Demo_and_Pages 12 | 13 | 14 | **Requires [mapbox-gl-js](https://github.com/mapbox/mapbox-gl-js) and [fontawesome](https://fontawesome.com/).** 15 | 16 | ### Installing 17 | 18 | ``` 19 | npm install mapbox-gl-fontawesome-markers 20 | ``` 21 | 22 | ### Usage in your application 23 | 24 | **When using modules** 25 | 26 | ```js 27 | import mapboxgl from 'mapbox-gl'; 28 | import FontawesomeMarker from "mapbox-gl-fontawesome-markers"; 29 | ``` 30 | 31 | **When using a CDN** 32 | 33 | ```html 34 | 35 | ``` 36 | 37 | ### Example usage 38 | 39 | ```js 40 | mapboxgl.accessToken = 'YOUR_ACCESS_TOKEN'; 41 | 42 | const map = new mapboxgl.Map({ 43 | container: 'map', 44 | style: 'mapbox://styles/mapbox/streets-v11', 45 | center: [40, -74.50], 46 | zoom: 9 47 | }); 48 | 49 | const marker = new FontawesomeMarker({ 50 | icon: 'fa-solid fa-pizza-slice', 51 | iconColor: 'steelblue', 52 | color: '#fa7132' 53 | }) 54 | .setLngLat([40, -74.50]) 55 | .addTo(map); 56 | ``` 57 | 58 | ## Development 59 | 60 | Install dependencies, build the bundle and load an example 61 | 62 | ``` 63 | npm install 64 | npm run build 65 | # serve `/example/static-html/index.html` using a local webserver 66 | ``` 67 | -------------------------------------------------------------------------------- /example/common/map-with-markers.js: -------------------------------------------------------------------------------- 1 | 2 | const icons = [ 3 | 'fa-solid fa-pizza-slice', 4 | 'fa-solid fa-hand-spock', 5 | 'fa-solid fa-hippo', 6 | 'fa-solid fa-road', 7 | 'fa-regular fa-face-grimace', 8 | 'fa-solid fa-fire', 9 | 'fa-solid fa-bicycle', 10 | 'fa-brands fa-android', 11 | 'fa-solid fa-sliders', 12 | 'fa-solid fa-flask', 13 | 'fa-solid fa-person', 14 | 'fa-solid fa-bath', 15 | 'fa-solid fa-earth-americas', 16 | 'fa-solid fa-gamepad', 17 | 'fa-solid fa-bug', 18 | 'fa-solid fa-fish', 19 | 'fa-solid fa-anchor', 20 | 'fa-solid fa-user-secret', 21 | 'fa-solid fa-microchip', 22 | 'fa-solid fa-robot', 23 | 'fa-brands fa-react', 24 | 'fa-solid fa-virus', 25 | 'fa-solid fa-medal', 26 | 'fa-solid fa-chair', 27 | 'fa-solid fa-plug', 28 | 'fa-brands fa-square-js', 29 | 'fa-regular fa-lightbulb', 30 | 'fa-brands fa-linux' 31 | ] 32 | 33 | const colors = ["#d9ed92", "#b5e48c", "#99d98c", "#76c893", "#52b69a", "#34a0a4", "#168aad", "#1a759f", "#1e6091", "#184e77"] 34 | const iconColors = ['crimson', 'darkslategray', 'indigo', 'midnightblue', 'paleturquoise', 'silver', 'darkgreen', 'whitesmoke', 'orange', 'yellow'] 35 | 36 | mapboxgl.accessToken = 'pk.eyJ1IjoiY2hyaXN3aG9uZ21hcGJveCIsImEiOiJjbDl6bzJ6N3EwMGczM3BudjZmbm5yOXFnIn0.lPhc5Z5H3byF_gf_Jz48Ug'; 37 | const map = new mapboxgl.Map({ 38 | container: 'map', 39 | // Choose from Mapbox's core styles, or make your own style with Mapbox Studio 40 | style: 'mapbox://styles/mapbox/dark-v11', 41 | zoom: 7, 42 | center: [-77.587241, 39.153318] 43 | }); 44 | 45 | map.on('load', () => { 46 | // Set the default atmosphere style 47 | map.setFog({}); 48 | 49 | 50 | icons.forEach((icon) => { 51 | const popup = new mapboxgl.Popup({ offset: 32 }).setText(icon); 52 | 53 | const lngLatFC = turf.randomPoint(1, { bbox: [-79.530448, 38.342557, -75.644034, 39.954844] }) 54 | 55 | const colorIndex = Math.floor(Math.random() * colors.length); 56 | const color = colors[colorIndex] 57 | const iconColor = iconColors[colorIndex] 58 | const scale = (Math.random() * (1.0000 - 2.0000) + 2.0000).toFixed(4) 59 | 60 | new FontawesomeMarker({ 61 | icon, 62 | color, 63 | iconColor, 64 | scale 65 | }) 66 | .setLngLat(lngLatFC.features[0].geometry.coordinates) 67 | .setPopup(popup) 68 | .addTo(map); 69 | }) 70 | }); 71 | --------------------------------------------------------------------------------