@maptiler/geocoding-control 0.0.38 → 0.0.42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/leaflet.js +14400 -1384
- package/dist/leaflet.umd.js +6 -3
- package/dist/lib/mask.d.ts +2 -0
- package/dist/maplibregl.js +14421 -1405
- package/dist/maplibregl.umd.js +6 -3
- package/package.json +14 -2
- package/src/lib/GeocodingControl.svelte +22 -5
- package/src/lib/leafletMapController.ts +13 -5
- package/src/lib/maplibreglMapController.ts +9 -5
- package/src/lib/mask.ts +69 -0
package/package.json
CHANGED
|
@@ -1,7 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@maptiler/geocoding-control",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.42",
|
|
4
4
|
"type": "module",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Martin Ždila",
|
|
7
|
+
"email": "martin.zdila@maptiler.com"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/maptiler/maptiler-geocoding-control"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/maptiler/maptiler-geocoding-control/issues"
|
|
15
|
+
},
|
|
5
16
|
"scripts": {
|
|
6
17
|
"dev": "vite",
|
|
7
18
|
"build": "vite build && mv dist/maplibregl.umd.cjs dist/maplibregl.umd.js && FLAVOUR=leaflet vite build && mv dist/leaflet.umd.cjs dist/leaflet.umd.js && tsc --outDir dist --declaration --emitDeclarationOnly",
|
|
@@ -34,7 +45,8 @@
|
|
|
34
45
|
"devDependencies": {
|
|
35
46
|
"@sveltejs/vite-plugin-svelte": "^1.1.0",
|
|
36
47
|
"@tsconfig/svelte": "^3.0.0",
|
|
37
|
-
"@turf/
|
|
48
|
+
"@turf/buffer": "^6.5.0",
|
|
49
|
+
"@turf/difference": "^6.5.0",
|
|
38
50
|
"@turf/union": "^6.5.0",
|
|
39
51
|
"@types/leaflet": "^1.9.0",
|
|
40
52
|
"prettier": "^2.7.1",
|
|
@@ -272,19 +272,17 @@
|
|
|
272
272
|
|
|
273
273
|
const sp = new URLSearchParams();
|
|
274
274
|
|
|
275
|
-
sp.set("key", apiKey);
|
|
276
|
-
|
|
277
275
|
if (language) {
|
|
278
276
|
sp.set("language", String(language));
|
|
279
277
|
}
|
|
280
278
|
|
|
281
279
|
if (!isReverse) {
|
|
282
280
|
if (bbox) {
|
|
283
|
-
sp.set("bbox", bbox.join(","));
|
|
281
|
+
sp.set("bbox", bbox.map((c) => c.toFixed(6)).join(","));
|
|
284
282
|
}
|
|
285
283
|
|
|
286
284
|
if (proximity) {
|
|
287
|
-
sp.set("proximity", proximity.join(","));
|
|
285
|
+
sp.set("proximity", proximity.map((c) => c.toFixed(6)).join(","));
|
|
288
286
|
}
|
|
289
287
|
|
|
290
288
|
// sp.set("autocomplete", String(autocomplete));
|
|
@@ -294,6 +292,8 @@
|
|
|
294
292
|
|
|
295
293
|
// sp.set("limit", String(limit));
|
|
296
294
|
|
|
295
|
+
sp.set("key", apiKey);
|
|
296
|
+
|
|
297
297
|
const url =
|
|
298
298
|
import.meta.env.VITE_API_URL +
|
|
299
299
|
"/" +
|
|
@@ -381,10 +381,27 @@
|
|
|
381
381
|
}
|
|
382
382
|
}
|
|
383
383
|
|
|
384
|
+
// taken from Leaflet
|
|
385
|
+
export function wrapNum(
|
|
386
|
+
x: number,
|
|
387
|
+
range: [number, number],
|
|
388
|
+
includeMax: boolean
|
|
389
|
+
) {
|
|
390
|
+
const max = range[1],
|
|
391
|
+
min = range[0],
|
|
392
|
+
d = max - min;
|
|
393
|
+
|
|
394
|
+
return x === max && includeMax ? x : ((((x - min) % d) + d) % d) + min;
|
|
395
|
+
}
|
|
396
|
+
|
|
384
397
|
function handleReverse(coordinates: [lng: number, lat: number]) {
|
|
385
398
|
reverseActive = enableReverse === "always";
|
|
386
399
|
|
|
387
|
-
setQuery(
|
|
400
|
+
setQuery(
|
|
401
|
+
wrapNum(coordinates[0], [-180, 180], true).toFixed(6) +
|
|
402
|
+
"," +
|
|
403
|
+
coordinates[1].toFixed(6)
|
|
404
|
+
);
|
|
388
405
|
}
|
|
389
406
|
|
|
390
407
|
function handleKeyDown(e: KeyboardEvent) {
|
|
@@ -6,9 +6,13 @@ import type {
|
|
|
6
6
|
MultiPolygon,
|
|
7
7
|
LineString,
|
|
8
8
|
MultiLineString,
|
|
9
|
+
Feature as TurfFeature,
|
|
10
|
+
Position,
|
|
9
11
|
} from "@turf/helpers";
|
|
10
|
-
import
|
|
12
|
+
import difference from "@turf/difference";
|
|
11
13
|
import union from "@turf/union";
|
|
14
|
+
import buffer from "@turf/buffer";
|
|
15
|
+
import { setMask } from "./mask";
|
|
12
16
|
|
|
13
17
|
export function createLeafletMapController(
|
|
14
18
|
map: L.Map,
|
|
@@ -19,12 +23,16 @@ export function createLeafletMapController(
|
|
|
19
23
|
fullGeometryStyle: L.PathOptions | L.StyleFunction = (feature) => {
|
|
20
24
|
const type = feature?.geometry?.type;
|
|
21
25
|
|
|
22
|
-
const weight =
|
|
26
|
+
const weight = feature?.properties?.isMask
|
|
27
|
+
? 0
|
|
28
|
+
: type === "LineString" || type === "MultiLineString"
|
|
29
|
+
? 3
|
|
30
|
+
: 2;
|
|
23
31
|
|
|
24
32
|
return {
|
|
25
33
|
color: "#3170fe",
|
|
26
34
|
fillColor: "#000",
|
|
27
|
-
fillOpacity: 0.1,
|
|
35
|
+
fillOpacity: feature?.properties?.isMask ? 0.1 : 0,
|
|
28
36
|
weight,
|
|
29
37
|
dashArray: [weight, weight],
|
|
30
38
|
lineCap: "butt",
|
|
@@ -167,7 +175,7 @@ export function createLeafletMapController(
|
|
|
167
175
|
| MultiPolygon; // union actually returns geometry
|
|
168
176
|
}
|
|
169
177
|
|
|
170
|
-
|
|
178
|
+
setMask({ ...picked, geometry }, setData);
|
|
171
179
|
|
|
172
180
|
handled = true;
|
|
173
181
|
} else {
|
|
@@ -194,7 +202,7 @@ export function createLeafletMapController(
|
|
|
194
202
|
picked.geometry.type === "Polygon" ||
|
|
195
203
|
picked.geometry.type === "MultiPolygon"
|
|
196
204
|
) {
|
|
197
|
-
|
|
205
|
+
setMask(picked as any, setData);
|
|
198
206
|
} else if (
|
|
199
207
|
picked.geometry.type === "LineString" ||
|
|
200
208
|
picked.geometry.type === "MultiLineString"
|
|
@@ -12,14 +12,18 @@ import type {
|
|
|
12
12
|
} from "maplibre-gl";
|
|
13
13
|
import MarkerIcon from "./MarkerIcon.svelte";
|
|
14
14
|
import type { Feature, MapController, Proximity } from "./types";
|
|
15
|
-
import mask from "@turf/mask";
|
|
16
15
|
import union from "@turf/union";
|
|
16
|
+
import buffer from "@turf/buffer";
|
|
17
|
+
import difference from "@turf/difference";
|
|
17
18
|
import type {
|
|
18
19
|
Polygon,
|
|
19
20
|
MultiPolygon,
|
|
20
21
|
LineString,
|
|
21
22
|
MultiLineString,
|
|
23
|
+
Feature as TurfFeature,
|
|
24
|
+
Position,
|
|
22
25
|
} from "@turf/helpers";
|
|
26
|
+
import { setMask } from "./mask";
|
|
23
27
|
|
|
24
28
|
let emptyGeojson: GeoJSON.FeatureCollection = {
|
|
25
29
|
type: "FeatureCollection",
|
|
@@ -38,11 +42,11 @@ export function createMaplibreglMapController(
|
|
|
38
42
|
line: Pick<LineLayerSpecification, "layout" | "paint" | "filter">;
|
|
39
43
|
} = {
|
|
40
44
|
fill: {
|
|
41
|
-
layout: {},
|
|
42
45
|
paint: {
|
|
43
46
|
"fill-color": "#000",
|
|
44
47
|
"fill-opacity": 0.1,
|
|
45
48
|
},
|
|
49
|
+
filter: ["all", ["==", ["geometry-type"], "Polygon"], ["has", "isMask"]],
|
|
46
50
|
},
|
|
47
51
|
line: {
|
|
48
52
|
layout: {
|
|
@@ -53,6 +57,7 @@ export function createMaplibreglMapController(
|
|
|
53
57
|
"line-dasharray": [1, 1],
|
|
54
58
|
"line-color": "#3170fe",
|
|
55
59
|
},
|
|
60
|
+
filter: ["!", ["has", "isMask"]],
|
|
56
61
|
},
|
|
57
62
|
}
|
|
58
63
|
) {
|
|
@@ -77,7 +82,6 @@ export function createMaplibreglMapController(
|
|
|
77
82
|
id: "full-geom-fill",
|
|
78
83
|
type: "fill",
|
|
79
84
|
source: "full-geom",
|
|
80
|
-
filter: ["==", ["geometry-type"], "Polygon"],
|
|
81
85
|
});
|
|
82
86
|
|
|
83
87
|
map.addLayer({
|
|
@@ -213,7 +217,7 @@ export function createMaplibreglMapController(
|
|
|
213
217
|
| MultiPolygon; // union actually returns geometry
|
|
214
218
|
}
|
|
215
219
|
|
|
216
|
-
|
|
220
|
+
setMask({ ...picked, geometry }, setData);
|
|
217
221
|
|
|
218
222
|
handled = true;
|
|
219
223
|
} else {
|
|
@@ -240,7 +244,7 @@ export function createMaplibreglMapController(
|
|
|
240
244
|
picked.geometry.type === "Polygon" ||
|
|
241
245
|
picked.geometry.type === "MultiPolygon"
|
|
242
246
|
) {
|
|
243
|
-
|
|
247
|
+
setMask(picked as any, setData);
|
|
244
248
|
} else if (
|
|
245
249
|
picked.geometry.type === "LineString" ||
|
|
246
250
|
picked.geometry.type === "MultiLineString"
|
package/src/lib/mask.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Polygon,
|
|
3
|
+
MultiPolygon,
|
|
4
|
+
Feature as TurfFeature,
|
|
5
|
+
Position,
|
|
6
|
+
} from "@turf/helpers";
|
|
7
|
+
import difference from "@turf/difference";
|
|
8
|
+
import buffer from "@turf/buffer";
|
|
9
|
+
|
|
10
|
+
// see https://maplibre.org/maplibre-gl-js-docs/example/line-across-180th-meridian/
|
|
11
|
+
function fixRing(ring: Position[]) {
|
|
12
|
+
let prev: Position | undefined = undefined;
|
|
13
|
+
|
|
14
|
+
for (const c of ring) {
|
|
15
|
+
if (prev && c[0] - prev[0] >= 180) {
|
|
16
|
+
c[0] -= 360;
|
|
17
|
+
} else if (prev && c[0] - prev[0] < -180) {
|
|
18
|
+
c[0] += 360;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
prev = c;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function setMask(
|
|
26
|
+
picked: TurfFeature<Polygon | MultiPolygon>,
|
|
27
|
+
setData: (data: GeoJSON.GeoJSON) => void
|
|
28
|
+
) {
|
|
29
|
+
const diff = difference(
|
|
30
|
+
{
|
|
31
|
+
type: "Polygon",
|
|
32
|
+
coordinates: [
|
|
33
|
+
[
|
|
34
|
+
[180, 90],
|
|
35
|
+
[-180, 90],
|
|
36
|
+
[-180, -90],
|
|
37
|
+
[180, -90],
|
|
38
|
+
[180, 90],
|
|
39
|
+
],
|
|
40
|
+
],
|
|
41
|
+
},
|
|
42
|
+
picked
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
if (!diff) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
diff.properties = { isMask: "y" };
|
|
50
|
+
|
|
51
|
+
const fixed = buffer(picked, 0);
|
|
52
|
+
|
|
53
|
+
if (fixed.geometry.type === "Polygon") {
|
|
54
|
+
for (const ring of fixed.geometry.coordinates) {
|
|
55
|
+
fixRing(ring);
|
|
56
|
+
}
|
|
57
|
+
} else {
|
|
58
|
+
for (const poly of fixed.geometry.coordinates) {
|
|
59
|
+
for (const ring of poly) {
|
|
60
|
+
fixRing(ring);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
setData({
|
|
66
|
+
type: "FeatureCollection",
|
|
67
|
+
features: [fixed, diff],
|
|
68
|
+
});
|
|
69
|
+
}
|