diff --git a/VKAPI/Handlers/Wall.php b/VKAPI/Handlers/Wall.php index e6897a0a..bb8e45b3 100644 --- a/VKAPI/Handlers/Wall.php +++ b/VKAPI/Handlers/Wall.php @@ -117,7 +117,7 @@ final class Wall extends VKAPIRequestHandler $geoarray = $post->getGeo(); $geo = [ - "coordinates" => $geoarray["lat"] . " " . $geoarray["lng"], + "coordinates" => $geoarray->lat . " " . $geoarray->lng, "showmap" => 1, "type" => "point" ]; @@ -392,7 +392,7 @@ final class Wall extends VKAPIRequestHandler ]; } - function post(string $owner_id, string $message = "", int $from_group = 0, int $signed = 0, string $attachments = "", float $latitude, float $longitude): object + function post(string $owner_id, string $message = "", int $from_group = 0, int $signed = 0, string $attachments = "", ?float $latitude = NULL, ?float $longitude = NULL, ?string $geo_name = NULL): object { $this->requireUser(); $this->willExecuteWriteAction(); @@ -433,11 +433,40 @@ final class Wall extends VKAPIRequestHandler if(empty($message) && empty($attachments)) $this->fail(100, "Required parameter 'message' missing."); - $geo = array( - "name" => null, - "lat" => $latitude, - "lng" => $longitude, - ); + $geo = NULL; + + if ($latitude && $longitude) { + $geo = array( + "name" => null, + "lat" => $latitude, + "lng" => $longitude, + ); + + if ($latitude > 90 || $latitude < -90 || $longitude > 180 || $longitude < -180) { + $this->fail(100, "Invalid latitude or longitude"); + } + + if (strlen(trim($geo_name))) { + $geo["name"] = $geo_name; + } else { + $info = file_get_contents("https://nominatim.openstreetmap.org/reverse?lat=${latitude}&lon=${longitude}&format=jsonv2", false, stream_context_create([ + 'http' => [ + 'method' => 'GET', + 'header' => implode("\r\n", [ + 'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2', + "Referer: https://$_SERVER[SERVER_NAME]/" + ]) + ] + ])); + + if ($info) { + $info = json_decode($info, true, JSON_UNESCAPED_UNICODE); + if (key_exists("place_id", $info)) { + $geo["name"] = $info["name"] ?? $info["display_name"]; + } + } + } + } try { $post = new Post; diff --git a/Web/Presenters/templates/components/post/microblogpost.xml b/Web/Presenters/templates/components/post/microblogpost.xml index c3a280d3..c2091fd2 100644 --- a/Web/Presenters/templates/components/post/microblogpost.xml +++ b/Web/Presenters/templates/components/post/microblogpost.xml @@ -76,7 +76,7 @@
- {_geotag}: {$post->getGeo()->name} + {_geotag}: {$post->getGeo()->name ?? tr("admin_open")}
diff --git a/Web/Presenters/templates/components/post/oldpost.xml b/Web/Presenters/templates/components/post/oldpost.xml index 590037e8..50180ae1 100644 --- a/Web/Presenters/templates/components/post/oldpost.xml +++ b/Web/Presenters/templates/components/post/oldpost.xml @@ -69,7 +69,9 @@
-
{_geotag}: {$post->getGeo()->name}
+
+ {_geotag}: {$post->getGeo()->name ?? tr("admin_open")} +

diff --git a/Web/static/js/al_wall.js b/Web/static/js/al_wall.js index ee767818..301342f8 100644 --- a/Web/static/js/al_wall.js +++ b/Web/static/js/al_wall.js @@ -353,8 +353,9 @@ function openGeo(data, owner_id, virtual_id) { let map = L.map(element, {attributionControl: false}); let target = L.latLng(data.lat, data.lng); map.setView(target, 15); + let marker = L.marker(target).addTo(map); - marker.bindPopup(data.name).openPopup(); + marker.bindPopup(data.name ?? tr("geotag")).openPopup(); L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', { attribution: '© OpenStreetMap contributors' @@ -404,7 +405,7 @@ function getPostPopup(post) {
-
${tr("geotag")}: ${post.geo.name}
+
${tr("geotag")}: ${post.geo.name ?? tr("admin_open")}
@@ -430,12 +431,27 @@ function openNearPosts(posts) { markerLayers.addTo(map); let markersBounds = []; + let coords = []; 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()); + if (coords.includes(`${post.geo.lat} ${post.geo.lng}`)) { + markerLayers.getLayers().forEach((marker) => { + if (marker.getLatLng().lat === post.geo.lat && marker.getLatLng().lng === post.geo.lng) { + let content = marker.getPopup()._content += getPostPopup(post); + if (!content.startsWith(`
`)) + content = `
${content}`; + + marker.getPopup().setContent(content); + } + }); + } else { + 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()); + } + + coords.push(`${post.geo.lat} ${post.geo.lng}`); }) let bounds = L.latLngBounds(markersBounds);