Карта ближайших постов

This commit is contained in:
n1rwana 2023-08-04 00:18:49 +03:00
parent f909976348
commit 753333d7f2
2 changed files with 44 additions and 12 deletions

View file

@ -3,8 +3,9 @@ SELECT *,
POW(69.1 * (? - geo_lat), 2) +
POW(69.1 * (? - geo_lon) * COS(RADIANS(geo_lat)), 2)
) AS distance
FROM Posts
FROM posts
WHERE id <> ?
AND FROM_UNIXTIME(created) >= DATE_SUB(NOW(), INTERVAL 1 MONTH)
HAVING distance < 1 AND distance IS NOT NULL
ORDER BY distance
LIMIT 25;

View file

@ -275,7 +275,7 @@ async function initGeo(tid) {
$(`#post-buttons${tid} .post-has-geo`).text(`${tr("geotag")}: ${marker.name}`);
$(`#post-buttons${tid} .post-has-geo`).show();
}), Function.noop]);
const element = document.getElementById('osm-map');
element.style = 'height: 600px;';
@ -378,12 +378,8 @@ function getNearPosts(owner_id, virtual_id) {
});
}
function openNearPosts(posts) {
console.log(posts);
let MsgBody = "";
posts.posts.forEach((post) => {
MsgBody += `<a style="color: inherit; display: block; margin-bottom: 8px;" href="${post.url}" target="_blank">
function getPostPopup(post) {
return `<a style="color: inherit; display: block; margin-bottom: 8px;" href="${post.url}" target="_blank">
<table border="0" style="font-size: 11px;" class="post">
<tbody>
<tr>
@ -416,9 +412,44 @@ function openNearPosts(posts) {
</tbody>
</table>
</a>`;
});
}
if (posts.need_count) MsgBody += "<br/><br/><center style='color: grey;'>Показаны первые 25 постов</center>"
function openNearPosts(posts) {
if (posts.posts.length > 0) {
let MsgTxt = "<div id=\"osm-map\"></div>";
MsgTxt += "<br /><br /><center style='color: grey;'>Показано последние 25 постов за месяц</center>";
MessageBox("Ближайшие посты", MsgBody, ["OK"], [Function.noop]);
}
MessageBox("Ближайшие посты", MsgTxt, ["OK"], [Function.noop]);
let element = document.getElementById('osm-map');
element.style = 'height: 600px;';
let markerLayers = L.layerGroup();
let map = L.map(element, {attributionControl: false});
markerLayers.addTo(map);
let markersBounds = [];
posts.posts.forEach((post) => {
let marker = L.marker(L.latLng(post.geo.lat, post.geo.lng)).addTo(map);
marker.bindPopup(getPostPopup(post));
markerLayers.addLayer(marker);
markersBounds.push(marker.getLatLng());
})
let bounds = L.latLngBounds(markersBounds);
map.fitBounds(bounds);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
$(".ovk-diag-cont").width('50%');
setTimeout(function () {
map.invalidateSize()
}, 100);
} else {
MessageBox("Ближайшие посты", "<center style='color: grey;'>Нет ближайших постов :(</center>", ["OK"], [Function.noop]);
}
}