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,24 +453,31 @@ 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)
$elem.html(`<div class="geo-map"></div>`) zoom = zoo
const map = L.map($elem.find('.geo-map')[0]).setView(position, zoom || 16) position = [lat, lng]
} else {
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { // parse value as address
attribution: '<a href="https://www.openstreetmap.org/">OSM</a>', const data = await fetch(`https://nominatim.openstreetmap.org/search?q=${encodeURIComponent(content)}&format=json`).then(r => r.json())
maxZoom: 18, const { lat, lon } = data[0]
}).addTo(map) position = [lat, lon]
L.marker(position).addTo(map)
$elem.addClass('geo')
} }
$elem.html(`<div class="geo-map"></div>`)
const map = L.map($elem.find('.geo-map')[0]).setView(position, zoom || 16)
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '<a href="https://www.openstreetmap.org/">OSM</a>',
maxZoom: 18,
}).addTo(map)
L.marker(position).addTo(map)
$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)