项目作者: NTag

项目描述 :
Create stylish SVG maps filled with dots
高级语言: JavaScript
项目地址: git://github.com/NTag/dotted-map.git
创建时间: 2020-05-04T20:04:50Z
项目社区:https://github.com/NTag/dotted-map

开源协议:MIT License

下载


npm version

dotted-map











You can limit to one (or several) countries (France, Italy, UK)

Installation

Requires NodeJS ≥ 13.

  1. npm i dotted-map

Usage

  1. const fs = require('fs');
  2. const DottedMap = require('dotted-map').default;
  3. // Or in the browser: import DottedMap from 'dotted-map';
  4. const map = new DottedMap({ height: 60, grid: 'diagonal' });
  5. map.addPin({
  6. lat: 40.73061,
  7. lng: -73.935242,
  8. svgOptions: { color: '#d6ff79', radius: 0.4 },
  9. });
  10. map.addPin({
  11. lat: 48.8534,
  12. lng: 2.3488,
  13. svgOptions: { color: '#fffcf2', radius: 0.4 },
  14. });
  15. const svgMap = map.getSVG({
  16. radius: 0.22,
  17. color: '#423B38',
  18. shape: 'circle',
  19. backgroundColor: '#020300',
  20. });
  21. fs.writeFileSync('./map.svg', svgMap);

If you use a large number of points (height or width ≥ 100), it may take a bit of time to compute the map (from 1 to 30 seconds depending on your device and number of points). This is why the result grid is cached. If you don’t change the parameters of new DottedMap, the next maps will be a lot faster to generate. You can however change the pins and the SVG options.

It’s also possible to use it in Leaflet, see an example here.

Precomputing the map

Because the previous operation can be expansive (especially if you want to use DottedMap in a browser or React Native app), it’s possible to precompute the grid. You will still be able to add pins on-the-fly, in real time. This also allows you to import a lighter version of the library. This is especially useful if you always use the same map parameters, but only change the pins.

  1. // So you do this first step only once, when developing your app
  2. const getMapJSON = require('dotted-map').getMapJSON;
  3. // This function accepts the same arguments as DottedMap in the example above.
  4. const mapJsonString = getMapJSON({ height: 60, grid: 'diagonal' });
  5. console.log(mapJsonString);
  6. // This string will contain everything about the grid. You will need to copy
  7. // and include it in your front.
  1. // Now we are in your app, let’s imagine it’s a React app
  2. // This import doesn’t include coordinates of countries: it’s lighter
  3. // that 'dotted-map', so especially useful in fronts.
  4. // However, you must give it a map you have pre-computed before.
  5. import DottedMap from 'dotted-map/without-countries';
  6. // Basically myMap.js contains something like:
  7. //
  8. // const MyMapString = 'the string mapJsonString that you got on the first step';
  9. // export default MyMapString;
  10. import MyMapString from './myMap';
  11. const MyComponent = () => {
  12. // It’s safe to re-create the map at each render, because of the
  13. // pre-computation it’s super fast ⚡️
  14. const map = new DottedMap({ map: JSON.parse(MyMapString) });
  15. map.addPin({
  16. lat: 40.73061,
  17. lng: -73.935242,
  18. svgOptions: { color: '#d6ff79', radius: 0.4 },
  19. });
  20. const svgMap = map.getSVG({
  21. radius: 0.22,
  22. color: '#423B38',
  23. shape: 'circle',
  24. backgroundColor: '#020300',
  25. });
  26. return (
  27. <div>
  28. <img src={`data:image/svg+xml;utf8,${encodeURIComponent(svgMap)}`} />
  29. </div>
  30. );
  31. };
  32. export default MyComponent;

That’s how you can display a super stylish map in your React webapp, without impacting the size of your bundle nor the performance of your app (browsers are very fast at rendering SVGs).

Specs

  1. import DottedMap from 'dotted-map';
  2. // Create the map
  3. const map = new DottedMap({
  4. height,
  5. width, // just specify either height or width, so the ratio of the map is correct
  6. countries: ['FRA'] // look into `countries.geo.json` to see which keys to use. You can also omit this parameter and the whole world will be used
  7. region: { lat: { min, max }, lng: { min, max } }, // if not present, it will fit the countries (and if no country is specified, the whole world)
  8. grid: 'vertical' | 'diagonal', // how points should be aligned
  9. avoidOuterPins: false | true, // if it’s true, prevent adding pins when they are outside of region/countries
  10. });
  11. // Add some points/change the color of existing points
  12. map.addPin({
  13. lat,
  14. lng,
  15. svgOptions: { color, radius },
  16. data, // whatever you want, useful if you use the method `getPoints` to get the raw points
  17. });
  18. // If you want to get the raw array of points
  19. map.getPoints();
  20. // [{ x, y, data, svgOptions }]
  21. // Or use this method to get a string which is a SVG
  22. map.getSVG({
  23. shape: 'circle' | 'hexagon', // if you use hexagon, prefer the grid `diagonal`
  24. backgroundColor, // background color of the map
  25. color, // default color of the points
  26. radius: 0.5, // default radius of the points
  27. });
  28. // <svg><circle … ></circle><circle …></svg>

Acknowledgments

Countries are from https://github.com/johan/world.geo.json.