@maptiler/geocoding-control 0.0.31

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/README.md ADDED
@@ -0,0 +1,165 @@
1
+ # MapTiler Geocoding control for MapLibre GL JS and Leaflet
2
+
3
+ A geocoding control for [maplibre-gl-js](https://github.com/maplibre/maplibre-gl-js) and [Leaflet](https://github.com/maplibre/maplibre-gl-js).
4
+
5
+ Component can be used as ES module or UMD module.
6
+
7
+ ## Usage
8
+
9
+ ### Usage with a module bundler
10
+
11
+ Example for [Maplibre GL](https://maplibre.org/maplibre-gl-js-docs/api/):
12
+
13
+ ```bash
14
+ npm install --save @maptiler/geocoding-control maplibre-gl
15
+ ```
16
+
17
+ ```js
18
+ import maplibregl from "maplibre-gl";
19
+ import { GeocodingControl } from "@maptiler/geocoding-control/maplibre";
20
+ import "@maptiler/geocoding-control/dist/style.css";
21
+
22
+ const API_KEY = "your API key";
23
+
24
+ const map = new maplibregl.Map({
25
+ container: "map", // id of HTML container element
26
+ style: "https://api.maptiler.com/maps/streets/style.json?key=" + API_KEY,
27
+ center: [16.3, 49.2],
28
+ zoom: 7,
29
+ });
30
+
31
+ const gc = new GeocodingControl({
32
+ apiKey: API_KEY,
33
+ maplibregl,
34
+ });
35
+
36
+ map.addControl(gc);
37
+ ```
38
+
39
+ Example for [Leaflet](https://leafletjs.com):
40
+
41
+ ```bash
42
+ npm install --save @maptiler/geocoding-control leaflet
43
+ ```
44
+
45
+ ```js
46
+ import * as L from "leaflet";
47
+ import { GeocodingControl } from "@maptiler/geocoding-control/leaflet";
48
+ import "@maptiler/geocoding-control/dist/style.css";
49
+
50
+ const API_KEY = "your API key";
51
+
52
+ const map = L.map(document.getElementById("map")).setView([49.2, 16.3], 6);
53
+
54
+ L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
55
+ attribution:
56
+ '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
57
+ }).addTo(map);
58
+
59
+ new GeocodingControl({ apiKey: API_KEY }).addTo(map);
60
+ ```
61
+
62
+ See [demo-maplibregl.html](./demo-maplibregl.html) or [demo-leaflet.html](./demo-leaflet.html) - after building this library (`npm install && npm run build`) open it in your browser with URL `file:///path_to_this_repository/demo.html#key=your_api_key`.
63
+
64
+ ## API Documentation
65
+
66
+ Options:
67
+
68
+ - `apiKey`<sup>\*</sup>: `string` - Maptiler API key
69
+ - `maplibregl`: `MapLibreGL` - A Maplibre GL instance to use when creating [Markers](https://maplibre.org/maplibre-gl-js-docs/api/markers/#marker). Required if `options.marker` is `true`. Used only with Maplibre GL library.
70
+ - `debounceSearch`: `number` - Sets the amount of time, in milliseconds, to wait before querying the server when a user types into the Geocoder input box. This parameter may be useful for reducing the total number of API calls made for a single query. Default `200`.
71
+ - `proximity`: `[number, number]` - A proximity argument: this is a geographical point given as an object with latitude and longitude properties. Search results closer to this point will be given higher priority.
72
+ - `placeholder`: `string` - Override the default placeholder attribute value. Default `"Search"`.
73
+ - `errorMessage`: `string` - Override the default error message. Default `"Searching failed"`.
74
+ - `noResultsMessage`: `string` - Override the default message if no results are found. Default `"No results found"`.
75
+ - `trackProximity`: `boolean` - If true, the geocoder proximity will automatically update based on the map view. Default `true`.
76
+ - `minLength`: `number` - Minimum number of characters to enter before results are shown. Default `2`.
77
+ - `bbox`: `[number, number, number, number]` - A bounding box argument: this is a bounding box given as an array in the format [minX, minY, maxX, maxY]. Search results will be limited to the bounding box.
78
+ - `language`: `string` - Specify the language to use for response text and query result weighting. Options are IETF language tags comprised of a mandatory ISO 639-1 language code and optionally one or more IETF subtags for country or script. More than one value can also be specified, separated by commas. Set to empty string for forcing no language preference.
79
+ - `showResultsWhileTyping`: `boolean` - If `false`, indicates that search will only occur on enter key press. If `true`, indicates that the Geocoder will search on the input box being updated above the minLength option. Default `false`.
80
+ - `marker`: `boolean | MarkerOptions` - If `true`, a [MapLibre GL Marker](https://maplibre.org/maplibre-gl-js-docs/api/markers/#marker) / [Leaflet Marker](https://leafletjs.com/reference.html#marker) will be added to the map at the location of the user-selected result using a default set of Marker options. If the value is an object, the marker will be constructed using these options. If `false`, no marker will be added to the map. Requires that `options.maplibregl` also be set. Default `true`.
81
+ - `showResultMarkers`: `boolean | MarkerOptions` - If `true`, [MapLibre GL Marker](https://maplibre.org/maplibre-gl-js-docs/api/markers/#marker) / [Leaflet Marker](https://leafletjs.com/reference.html#marker) will be added to the map at the location the top results for the query. If the value is an object, the marker will be constructed using these options. If `false`, no marker will be added to the map. Requires that `options.maplibregl` also be set. Default `true`.
82
+ - `zoom`: `number` - On geocoded result what zoom level should the map animate to when a bbox isn't found in the response. If a bbox is found the map will fit to the bbox. Default `16`.
83
+ - `flyTo`: `boolean | (FlyToOptions & FitBoundsOptions)` - If `false`, animating the map to a selected result is disabled. If `true`, animating the map will use the default animation parameters. If an object, it will be passed as options to the map `flyTo` or `fitBounds` method providing control over the animation of the transition. Default `true`.
84
+ - `collapsed`: `boolean` - If `true`, the geocoder control will collapse until hovered or in focus. Default `false`.
85
+ - `clearOnBlur`: `boolean` - If true, the geocoder control will clear its value when the input blurs. Default `false`.
86
+ - `filter`: `(feature: Feature) => boolean` - A function which accepts a Feature in the Carmen GeoJSON format to filter out results from the Geocoding API response before they are included in the suggestions list. Return true to keep the item, false otherwise.
87
+ - `class`: `string` - Class of the root element.
88
+ - `enableReverse`: `boolean | string` - Set to `true` to enable reverse geocoding button with title _toggle reverse geocoding_ or set the button title directly. Default `false`.
89
+
90
+ Methods:
91
+
92
+ - `setQuery(value: string, submit = true): void` - set the query and optionally submit it
93
+ - `focus(): void` - focus the query input box
94
+ - `blur(): void` - blur the query input box
95
+ - `setReverseMode(value: boolean): void` - set reverse mode
96
+
97
+ Events:
98
+
99
+ - `select` - Fired on highlighting search result in the dropdown by hovering it or by keyboard selection. Event value will be set to the highlighted `Feature` or to `undefined` if nothing is highlighted.
100
+ - `pick` - Fired on picking the result from the dropdown. Event value will be set to the picked `Feature` or to `undefined` if nothing is picked (eg. search input is cleared).
101
+ - `optionsVisibilityChange` - Fired with `true` value if dropdown list appears, `false` if it disappears
102
+ - `featuresListed` - Fired after features are retrieved from the server. Event value contains list of features or `undefined`.
103
+ - `featuresMarked` - Fired after features are marked on the map. Event value contains list of features or `undefined`.
104
+ - `response` - Fired after HTTP response of the geocoding server. Event value contains object with requested `url` and responded `featureCollection`.
105
+ - `reversetoggle` - Fired if reverse geocoding button is toggled. Event value is `true` if reverse geocoding mode is active, otherwise `false`.
106
+ - `querychange` - Fired if query was changed. Event value is the query string.
107
+
108
+ ## Svelte component
109
+
110
+ In addition to using the component as MapLibre GL Control it is alto possible to use it stand-alone in Svelte projects.
111
+ Component API matches API described above and options are exposed as component properties.
112
+
113
+ ```svelte
114
+ <script lang="ts">
115
+ import GeocodingControl from "@maptiler/geocoding-control/src/lib/GeocodingControl.svelte";
116
+ import GeocodingControl from "@maptiler/geocoding-control/src/lib/GeocodingControl.svelte";
117
+ import { createMaplibreMapController } from "@maptiler/geocoding-control/src/lib/maplibreMapController";
118
+ import type { MapController } from "@maptiler/geocoding-control/src/lib/types";
119
+ import maplibregl from "maplibre-gl";
120
+ import "maplibre-gl/dist/maplibre-gl.css";
121
+
122
+ const apiKey = "your API key";
123
+
124
+ let mapController: MapController;
125
+
126
+ let container: HTMLElement;
127
+
128
+ onMount(() => {
129
+
130
+ const map = new maplibregl.Map({
131
+ style: "https://api.maptiler.com/maps/streets/style.json?key=" + apiKey,
132
+ container,
133
+ });
134
+
135
+ const mapController = createMaplibreMapController(map, maplibregl);
136
+ }
137
+ </script>
138
+
139
+ <div class="map" bind:this={container} />
140
+
141
+ {#if mapController}
142
+ <GeocodingControl {mapController} {apiKey} {maplibregl} />
143
+ {/if}
144
+
145
+ <style>
146
+ .map {
147
+ position: absolute;
148
+ inset: 0;
149
+ }
150
+ </style>
151
+ ```
152
+
153
+ ## Building
154
+
155
+ ```bash
156
+ npm install && npm run build
157
+ ```
158
+
159
+ You will find compilation result in `dist` directory.
160
+
161
+ ## Running in dev mode
162
+
163
+ ```bash
164
+ npm install && VITE_API_KEY=your_api_key npm run dev
165
+ ```