Skip to content

Commit

Permalink
Added adjustUrl function (#71)
Browse files Browse the repository at this point in the history
* added `adjustUrl` function

* added `adjustUrl` example

* updated documentations
  • Loading branch information
zdila authored Dec 9, 2024
1 parent 919af24 commit ada0c74
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
7 changes: 7 additions & 0 deletions examples/maplibregl/AppMapLibreGl.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
{ type: "client-geolocation", minZoom: 8 },
{ type: "server-geolocation", minZoom: 8 },
],
adjustUrl(url) {
// for reverse geocoding use only address type
if (/\/\d+\.?\d*%2C\d+.?\d*.json$/.test(url.pathname)) {
url.searchParams.set("types", "address");
url.searchParams.set("limit", "5");
}
},
marker(map, feature) {
if (!feature) {
return;
Expand Down
34 changes: 23 additions & 11 deletions src/GeocodingControl.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,16 @@
import.meta.env.VITE_LIB_VERSION +
"/icons/";
/**
* @deprecated use `adjustUrl`
*/
export let adjustUrlQuery: (sp: URLSearchParams) => void = () => {};
/**
* Adjust geocoding URL before the fetch.
*/
export let adjustUrl: (url: URL) => void = () => {};
/**
* Focus the search input box.
*
Expand Down Expand Up @@ -458,7 +466,18 @@
try {
const isReverse = isQueryReverse(searchValue);
const sp = new URLSearchParams();
const urlObj = new URL(
apiUrl +
"/" +
encodeURIComponent(
isReverse
? isReverse.decimalLongitude + "," + isReverse.decimalLatitude
: searchValue,
) +
".json",
);
const sp = urlObj.searchParams;
if (language !== undefined) {
sp.set(
Expand Down Expand Up @@ -508,16 +527,9 @@
adjustUrlQuery(sp);
const url =
apiUrl +
"/" +
encodeURIComponent(
isReverse
? isReverse.decimalLongitude + "," + isReverse.decimalLatitude
: searchValue,
) +
".json?" +
sp.toString();
adjustUrl(urlObj);
const url = urlObj.toString();
if (url === lastSearchUrl) {
if (byId) {
Expand Down
1 change: 1 addition & 0 deletions src/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type MapControllerProp = {

const propertyNames = [
"adjustUrlQuery",
"adjustUrl",
"apiKey",
"bbox",
"clearButtonTitle",
Expand Down
11 changes: 11 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,20 @@ export type ControlOptions = {
* Function to adjust URL search parameters.
*
* Default value is empty function.
*
* @deprecated use `adjustUrl`
*/
adjustUrlQuery?: (sp: URLSearchParams) => void;

/**
* Function to adjust geocoding URL before the fetch.
*
* @param url [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL) parameter which can be modified
*
* Default value is empty function.
*/
adjustUrl?: (url: URL) => void;

/**
* Automatically select the first feature from the result list.
*
Expand Down

0 comments on commit ada0c74

Please sign in to comment.