Handle geo address lookup

Signed-off-by: Yukai Huang <yukaihuangtw@gmail.com>
This commit is contained in:
Yukai Huang 2019-09-28 19:14:33 +08:00
parent d9851f2a28
commit 0bb3e2614b
No known key found for this signature in database
GPG Key ID: D4D3B2F0E99D4914
1 changed files with 20 additions and 13 deletions

View File

@ -453,14 +453,22 @@ export function finishView (view) {
console.warn(err) console.warn(err)
} }
}) })
view.find('div.geo.raw').removeClass('raw').each(function (key, value) { view.find('div.geo.raw').removeClass('raw').each(async function (key, value) {
const $elem = $(value).parent().parent() const $elem = $(value).parent().parent()
const $value = $(value) const $value = $(value)
const content = $value.text() const content = $value.text()
try { try {
if (content.match(/[\d\.\,]+/)) { let position, zoom
const [lng, lat, zoom] = content.split(',').map(parseFloat) if (content.match(/^[\d\.\,\s]+$/)) {
const position = [lat, lng] const [lng, lat, zoo] = content.split(',').map(parseFloat)
zoom = zoo
position = [lat, lng]
} else {
// parse value as address
const data = await fetch(`https://nominatim.openstreetmap.org/search?q=${encodeURIComponent(content)}&format=json`).then(r => r.json())
const { lat, lon } = data[0]
position = [lat, lon]
}
$elem.html(`<div class="geo-map"></div>`) $elem.html(`<div class="geo-map"></div>`)
const map = L.map($elem.find('.geo-map')[0]).setView(position, zoom || 16) const map = L.map($elem.find('.geo-map')[0]).setView(position, zoom || 16)
@ -470,7 +478,6 @@ export function finishView (view) {
}).addTo(map) }).addTo(map)
L.marker(position).addTo(map) L.marker(position).addTo(map)
$elem.addClass('geo') $elem.addClass('geo')
}
} catch (err) { } catch (err) {
$elem.append(`<div class="alert alert-warning">${escapeHTML(err)}</div>`) $elem.append(`<div class="alert alert-warning">${escapeHTML(err)}</div>`)
console.warn(err) console.warn(err)