Skip to content

Commit

Permalink
add support for hogfish forward geocoder
Browse files Browse the repository at this point in the history
  • Loading branch information
Natalia Kowalczyk committed Jan 25, 2025
1 parent cf1dca7 commit b5218c4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
19 changes: 12 additions & 7 deletions demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ function onMapInit() {
searchFormEl.addEventListener('submit', function (event) {
event.preventDefault();
const { place, ll, type, max, partial } = searchFormEl;
if (place) {
if (ll.value) {
const detail = {
ll: ll.value.split(',').map(l => parseFloat(l.trim())),
max: max.value,
type: place.value
};
window.dispatchEvent(new CustomEvent('ll', { detail }));
}
else if (place.value) {
const detail = {
bounds: map.bounds(),
max: max.value,
Expand All @@ -44,12 +52,6 @@ function onMapInit() {
detail.partial = true;
}
window.dispatchEvent(new CustomEvent('place', { detail }));
} else if (ll.value) {
const detail = {
ll: ll.value.split(',').map(l => parseFloat(l.trim())),
max: max.value
};
window.dispatchEvent(new CustomEvent('ll', { detail }));
}
});
}
Expand Down Expand Up @@ -83,6 +85,9 @@ if (process.env.HOGFISH_KEY) {
],
fillingstation: [
'provider=fuel'
],
place: [
'provider=universal'
]
}
},
Expand Down
15 changes: 13 additions & 2 deletions lib/service/hogfish/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,29 @@ function getUrl(url, types, op, query) {
case 'reverse':
q.push('ll=' + query.ll.join(','));
q.push('radius=100');
q.push(...types[query.type]);
break;
default:
case 'forward':
q.push('q=' + encodeURIComponent(query.place || query.address));
if (query.bounds) {
q.push('sw=' + query.bounds[0].join(','));
q.push('ne=' + query.bounds[1].join(','));
}
break;
default:
// invalid operation
return;
}
q.push(...types[query.type]);
if (query.max) {
q.push('limit=' + query.max);
}
return url + '?' + q.join('&');
}

function prepareRequest(types, op, query) {
if (op === 'forward') {
query.type = query.type ?? ['place', 'address'].find(type => query.hasOwnProperty(type));
}
return Boolean(types[query.type]);
}

Expand Down Expand Up @@ -115,6 +125,7 @@ function init(options) {
}
options = util.defaults(options, {
reverse: true,
forward: true,
url: getUrl.bind(undefined, options.hogfish_url, options.types),
status: getStatus,
prepareRequest: prepareRequest.bind(undefined, options.types),
Expand Down

0 comments on commit b5218c4

Please sign in to comment.