Skip to content

Commit

Permalink
feat: add handling of lat, lon coordinate queries (#368)
Browse files Browse the repository at this point in the history
Quick, *pure js* implementation to handle coordinate queries (#147). If
a user enters valid coordinates, they get a single result with that
point (as in Google Maps and other services). It's a useful feature for
power users.

![image](https://github.com/smeijer/leaflet-geosearch/assets/8945883/775ac7a2-3802-44e8-a3d5-c59d91d579e6)

Any other queries are sent to the Provider. Happy to see this in
TypeScript and otherwise improved, it's probably not ready for merging.

---------

Co-authored-by: Stephan Meijer <[email protected]>
  • Loading branch information
alexandervlpl and smeijer authored Oct 4, 2023
1 parent f321ef5 commit 1f19d1a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/SearchControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ControlPosition, FeatureGroup, MarkerOptions, Map } from 'leaflet';
import SearchElement from './SearchElement';
import ResultList from './resultList';
import debounce from './lib/debounce';
import { validateCoords } from './coords';

import {
createElement,
Expand Down Expand Up @@ -373,7 +374,13 @@ const Control: SearchControl = {
const { provider } = this.options;

if (query.length) {
let results = await provider!.search({ query });
let results = [];
const coords = validateCoords(query);
if (coords) {
results = coords;
} else {
results = await provider!.search({ query });

Check warning on line 382 in src/SearchControl.ts

View workflow job for this annotation

GitHub Actions / Eslint

Forbidden non-null assertion
}
results = results.slice(0, this.options.maxSuggestions);
this.resultList.render(results, this.options.resultFormat);
} else {
Expand All @@ -385,7 +392,13 @@ const Control: SearchControl = {
this.resultList.clear();
const { provider } = this.options;

const results = await provider!.search(query);
let results = [];
const coords = validateCoords(query);
if (coords) {
results = coords;
} else {
results = await provider!.search(query);

Check warning on line 400 in src/SearchControl.ts

View workflow job for this annotation

GitHub Actions / Eslint

Forbidden non-null assertion
}

if (results && results.length > 0) {
this.showResult(results[0], query);
Expand Down
23 changes: 23 additions & 0 deletions src/coords.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// @ts-nocheck

export function validateCoords(query) {
const q = query.trim();
const regex = /^(-?[0-9]*\.?\s*[0-9]*)\s*,?\s*(-?[0-9]*\.?[0-9]*)$/g;
const match = regex.exec(q);
if (match) {
const lat = Number(match[1]);
const lng = Number(match[2]);
if (-90 < lat < 90 && -180 < lng < 180) {
return [
{
x: lng,
y: lat,
label: q,
bounds: null,
raw: {},
},
];
}
}
return false;
}

0 comments on commit 1f19d1a

Please sign in to comment.