Coder Social home page Coder Social logo

dotted-map's Introduction

npm version

dotted-map


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

Installation

Requires NodeJS ≥ 13.

npm i dotted-map

Usage

const fs = require('fs');
const DottedMap = require('dotted-map').default;
// Or in the browser: import DottedMap from 'dotted-map';

const map = new DottedMap({ height: 60, grid: 'diagonal' });

map.addPin({
  lat: 40.73061,
  lng: -73.935242,
  svgOptions: { color: '#d6ff79', radius: 0.4 },
});
map.addPin({
  lat: 48.8534,
  lng: 2.3488,
  svgOptions: { color: '#fffcf2', radius: 0.4 },
});

const svgMap = map.getSVG({
  radius: 0.22,
  color: '#423B38',
  shape: 'circle',
  backgroundColor: '#020300',
});

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.

// So you do this first step only once, when developing your app
const getMapJSON = require('dotted-map').getMapJSON;

// This function accepts the same arguments as DottedMap in the example above.
const mapJsonString = getMapJSON({ height: 60, grid: 'diagonal' });
console.log(mapJsonString);

// This string will contain everything about the grid. You will need to copy
// and include it in your front.
// Now we are in your app, let’s imagine it’s a React app

// This import doesn’t include coordinates of countries: it’s lighter
// that 'dotted-map', so especially useful in fronts.
// However, you must give it a map you have pre-computed before.
import DottedMap from 'dotted-map/without-countries';

// Basically myMap.js contains something like:
//
// const MyMapString = 'the string mapJsonString that you got on the first step';
// export default MyMapString;
import MyMapString from './myMap';

const MyComponent = () => {
  // It’s safe to re-create the map at each render, because of the
  // pre-computation it’s super fast ⚡️
  const map = new DottedMap({ map: JSON.parse(MyMapString) });

  map.addPin({
    lat: 40.73061,
    lng: -73.935242,
    svgOptions: { color: '#d6ff79', radius: 0.4 },
  });

  const svgMap = map.getSVG({
    radius: 0.22,
    color: '#423B38',
    shape: 'circle',
    backgroundColor: '#020300',
  });

  return (
    <div>
      <img src={`data:image/svg+xml;utf8,${encodeURIComponent(svgMap)}`} />
    </div>
  );
};

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

import DottedMap from 'dotted-map';

// Create the map
const map = new DottedMap({
  height,
  width, // just specify either height or width, so the ratio of the map is correct
  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
  region: { lat: { min, max }, lng: { min, max } }, // if not present, it will fit the countries (and if no country is specified, the whole world)
  grid: 'vertical' | 'diagonal', // how points should be aligned
  avoidOuterPins: false | true, // if it’s true, prevent adding pins when they are outside of region/countries
});

// Add some points/change the color of existing points
map.addPin({
  lat,
  lng,
  svgOptions: { color, radius },
  data, // whatever you want, useful if you use the method `getPoints` to get the raw points
});

// If you want to get the raw array of points
map.getPoints();
// [{ x, y, data, svgOptions }]

// Or use this method to get a string which is a SVG
map.getSVG({
  shape: 'circle' | 'hexagon', // if you use hexagon, prefer the grid `diagonal`
  backgroundColor, // background color of the map
  color, // default color of the points
  radius: 0.5, // default radius of the points
});
// <svg><circle … /><circle …></svg>

Acknowledgments

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

dotted-map's People

Contributors

ntag avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

dotted-map's Issues

Custom popup on markers

Is it possible to add custom popup on markers of the hexagon map?
I am just gonna add a popup on markers on the hexagon map.

Change Projection, embed within D3 or similar

Hey there! First of all - love this project!

I'd need to use similar dots around an orthographic globe projection. I'm wondering: Is there any way that this could be used more flexibly?

Things I tried: getting the JSON string to be used as geoJson. Draw the SVG using d3js. So far, I did not really succeed.

What would be the easiest way to change the project of this and/or make this more flexible?

Thanks so much,
Arne

Error: "poly is not defined"

I get the mentioned error when I try to add a Pin to the map. Here is the code I use:

const createMap = () => {
    const map = new DottedMap({
        height: 60,
        grid: 'vertical',
        avoidOuterPins: true,
    });

    map.addPin({
        lat: 40.73061,
        lng: -73.935242,
        svgOptions: { color: '#d6ff79', radius: 0.4 },
    });

    mapSvg.value = map.getSVG({
        shape: 'circle',
        color: '#FFFFFF',
        radius: 0.22,
    });
}

onMounted(() => createMap())

And this is the error message:

Uncaught (in promise) ReferenceError: poly is not defined
    at Object.getPin (index.js:1:259757)
    at Object.addPin (index.js:1:259544)
    at createMap (statistics.vue:23:11)
    at statistics.vue:38:21
    at chunk-FBYHIP7F.js:4075:88
    at callWithErrorHandling (chunk-FBYHIP7F.js:1565:18)
    at callWithAsyncErrorHandling (chunk-FBYHIP7F.js:1573:17)
    at hook.__weh.hook.__weh (chunk-FBYHIP7F.js:4055:19)
    at flushPostFlushCbs (chunk-FBYHIP7F.js:1731:41)
    at flushJobs (chunk-FBYHIP7F.js:1769:5)

Any ideas?

Country extraction

Thanks for the nice library!
One problem i stumbled upon: I tried to extract a specific country svg, but when i do, it throws an error:

`c:\dev\map>node map.js
c:\dev\map\node_modules\dotted-map\index.js:1
(function (exports, require, module, __filename, __dirname) { !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("proj4"),require("@turf/boolean-point-in-polygon")):"function"==typeof define&&define.amd?define(["proj4","@turf/boolean-point-in-polygon"],t):"object"==typeof exports?exports["dotted-map"]=t(require("proj4"),require("@turf/boolean-point-in-polygon")):e["dotted-map"]=t(e.proj4,e["@turf/boolean-point-in-polygon"])}(this,(function(e,t){return(()=>{"use strict";var o={573:(e,t,o)=>{o.r(t),o.d(t,{default:()=>c,getMapJSON:()=>u});var r=o(25),a=o.n(r),n=o(620),i=o.n(n);const p=JSON.parse('{"type":"FeatureCollection","features":[{"type":"Feature","id":"AFG","properties":{"name":"Afghanistan"},"geometry":{"type":"Polygon","coordinates":[[[61.210817,35.650072],[62.230651,35.270664],[62.984662,35.404041],[63.193538,35.857166],[63.982896,36.007957],[64.546479,36.312073],[64.746105,37.111818],[65.588948,37.305217],[65.745631

TypeError: a.flat is not a function
at m (c:\dev\map\node_modules\dotted-map\index.js:1:257925)
at m (c:\dev\map\node_modules\dotted-map\index.js:1:257823)
at Array.map ()
at m (c:\dev\map\node_modules\dotted-map\index.js:1:257617)
at l (c:\dev\map\node_modules\dotted-map\index.js:1:258331)
at new c (c:\dev\map\node_modules\dotted-map\index.js:1:259269)
at Object. (c:\dev\map\map.js:26:13)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)`

This is the code i used:

`const fs = require('fs');
const DottedMap = require('dotted-map').default;

const map = new DottedMap(
{
height: 100,
grid: 'vertical',//'diagonal',
countries: ['FRA'],
avoidOuterPins: false
});

const svgMap = map.getSVG({
radius: 0.22,
color: '#000',
shape: 'circle',
});

console.log(svgMap);`

Did i miss something?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.