From d95b824138529ab0aaf64977063d8e34c00456f6 Mon Sep 17 00:00:00 2001 From: Basile Bruneau Date: Sat, 12 Feb 2022 20:21:08 +0100 Subject: [PATCH] feat: add getPin method --- index.d.ts | 3 +++ package-lock.json | 5 +++-- package.json | 2 +- src/without-countries.js | 18 +++++++++++++++--- without-countries.d.ts | 5 +++++ 5 files changed, 27 insertions(+), 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index 6a8558f..94730ff 100644 --- a/index.d.ts +++ b/index.d.ts @@ -38,6 +38,8 @@ namespace DottedMapLib { type Point = { x: number; y: number; + lat: number; + lng: number; data?: any; svgOptions?: SvgOptions; }; @@ -49,6 +51,7 @@ export default class DottedMap { constructor(settings: DottedMapLib.Settings); addPin(pin: DottedMapLib.Pin): DottedMapLib.Point; + getPin(pin: DottedMapLib.Pin): DottedMapLib.Point; getPoints(): DottedMapLib.Point[]; getSVG(settings: DottedMapLib.SvgSettings): string; image: { diff --git a/package-lock.json b/package-lock.json index 093fc5e..008435a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,12 @@ { "name": "dotted-map", - "version": "2.1.0", + "version": "2.2.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "2.1.0", + "name": "dotted-map", + "version": "2.2.0", "license": "MIT", "dependencies": { "@turf/boolean-point-in-polygon": "^6.0.1", diff --git a/package.json b/package.json index 182ef67..76e87aa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dotted-map", - "version": "2.1.1", + "version": "2.2.0", "description": "Create a SVG map filled with dots for the world or countries", "main": "./index.js", "exports": { diff --git a/src/without-countries.js b/src/without-countries.js index 55b1351..fb2a71f 100644 --- a/src/without-countries.js +++ b/src/without-countries.js @@ -17,6 +17,14 @@ function DottedMapWithoutCountries({ map, avoidOuterPins = false }) { return { addPin({ lat, lng, data, svgOptions }) { + const pin = getPin({ lat, lng }); + const point = { ...pin, data, svgOptions }; + + points[[x, y].join(';')] = point; + + return point; + }, + getPin({ lat, lng }) { const [googleX, googleY] = proj4(proj4.defs('GOOGLE'), [lng, lat]); if (avoidOuterPins) { const wgs84Point = proj4(proj4.defs('GOOGLE'), proj4.defs('WGS84'), [ @@ -39,11 +47,15 @@ function DottedMapWithoutCountries({ map, avoidOuterPins = false }) { localx += 0.5; } - const point = { x: localx, y: localy, data, svgOptions }; + const [localLng, localLat] = proj4( + proj4.defs('GOOGLE'), + proj4.defs('WGS84'), + [localx, localy], + ); - points[[x, y].join(';')] = point; + const pin = { x: localx, y: localy, lat: localLat, lng: localLng }; - return point; + return pin; }, getPoints() { return Object.values(points); diff --git a/without-countries.d.ts b/without-countries.d.ts index 6fdb6ce..5afc9ae 100644 --- a/without-countries.d.ts +++ b/without-countries.d.ts @@ -44,6 +44,8 @@ namespace DottedMapWithoutCountriesLib { type Point = { x: number; y: number; + lat: number; + lng: number; data?: any; svgOptions?: SvgOptions; }; @@ -55,6 +57,9 @@ export default class DottedMapWithoutCountries { addPin( pin: DottedMapWithoutCountriesLib.Pin, ): DottedMapWithoutCountriesLib.Point; + getPin( + pin: DottedMapWithoutCountriesLib.Pin, + ): DottedMapWithoutCountriesLib.Point; getPoints(): DottedMapWithoutCountriesLib.Point[]; getSVG(settings: DottedMapWithoutCountriesLib.SvgSettings): string; image: {