Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add handling of lat, lon coordinate queries #368

Merged
merged 15 commits into from
Oct 4, 2023
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 });
var results = [];
smeijer marked this conversation as resolved.
Show resolved Hide resolved
const coords = validateCoords({query});
smeijer marked this conversation as resolved.
Show resolved Hide resolved
if (coords) {
results = coords;
} else {
results = await provider!.search({query});
smeijer marked this conversation as resolved.
Show resolved Hide resolved
}
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);
var results = [];
smeijer marked this conversation as resolved.
Show resolved Hide resolved
const coords = validateCoords(query);
if (coords) {
results = coords;
smeijer marked this conversation as resolved.
Show resolved Hide resolved
} else {
results = await provider!.search(query);
smeijer marked this conversation as resolved.
Show resolved Hide resolved
}

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

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