Better geo points

This commit is contained in:
n1rwana 2023-08-04 02:10:28 +03:00
parent 92690767a1
commit 5a7c99be74
4 changed files with 62 additions and 15 deletions

View file

@ -117,7 +117,7 @@ final class Wall extends VKAPIRequestHandler
$geoarray = $post->getGeo(); $geoarray = $post->getGeo();
$geo = [ $geo = [
"coordinates" => $geoarray["lat"] . " " . $geoarray["lng"], "coordinates" => $geoarray->lat . " " . $geoarray->lng,
"showmap" => 1, "showmap" => 1,
"type" => "point" "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->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
@ -433,11 +433,40 @@ final class Wall extends VKAPIRequestHandler
if(empty($message) && empty($attachments)) if(empty($message) && empty($attachments))
$this->fail(100, "Required parameter 'message' missing."); $this->fail(100, "Required parameter 'message' missing.");
$geo = array( $geo = NULL;
"name" => null,
"lat" => $latitude, if ($latitude && $longitude) {
"lng" => $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 { try {
$post = new Post; $post = new Post;

View file

@ -76,7 +76,7 @@
<div n:if="$post->getGeo()" class="post-signature"> <div n:if="$post->getGeo()" class="post-signature">
<a onclick="javascript:openGeo({$post->getGeo()}, {$post->getOwner()->getId()}, {$post->getVirtualId()})"> <a onclick="javascript:openGeo({$post->getGeo()}, {$post->getOwner()->getId()}, {$post->getVirtualId()})">
<img src="/assets/packages/static/openvk/img/geo.svg"> <img src="/assets/packages/static/openvk/img/geo.svg">
<b>{_geotag}</b>: {$post->getGeo()->name} <b>{_geotag}</b>: {$post->getGeo()->name ?? tr("admin_open")}
</a> </a>
</div> </div>
<div n:if="$post->isAd()" style="color:grey;"> <div n:if="$post->isAd()" style="color:grey;">

View file

@ -69,7 +69,9 @@
</div> </div>
<div n:if="$post->getGeo()" style="padding: 4px;"> <div n:if="$post->getGeo()" style="padding: 4px;">
<div style="border-bottom: #ECECEC solid 1px;" /> <div style="border-bottom: #ECECEC solid 1px;" />
<div style="cursor: pointer; padding: 4px;" onclick="javascript:openGeo({$post->getGeo()}, {$post->getOwner()->getId()}, {$post->getVirtualId()})"><b>{_geotag}</b>: {$post->getGeo()->name}</div> <div style="cursor: pointer; padding: 4px;" onclick="javascript:openGeo({$post->getGeo()}, {$post->getOwner()->getId()}, {$post->getVirtualId()})">
<b>{_geotag}</b>: <a>{$post->getGeo()->name ?? tr("admin_open")}</a>
</div>
</div> </div>
<div n:if="$post->isAd()" style="color:grey;"> <div n:if="$post->isAd()" style="color:grey;">
<br/> <br/>

View file

@ -353,8 +353,9 @@ function openGeo(data, owner_id, virtual_id) {
let map = L.map(element, {attributionControl: false}); let map = L.map(element, {attributionControl: false});
let target = L.latLng(data.lat, data.lng); let target = L.latLng(data.lat, data.lng);
map.setView(target, 15); map.setView(target, 15);
let marker = L.marker(target).addTo(map); 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', { L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
@ -404,7 +405,7 @@ function getPostPopup(post) {
</div> </div>
<div style="padding: 4px;"> <div style="padding: 4px;">
<div style="border-bottom: #ECECEC solid 1px;"></div> <div style="border-bottom: #ECECEC solid 1px;"></div>
<div style="cursor: pointer; padding: 4px;"><b>${tr("geotag")}</b>: ${post.geo.name}</div> <div style="cursor: pointer; padding: 4px;"><b>${tr("geotag")}</b>: ${post.geo.name ?? tr("admin_open")}</div>
</div> </div>
</div> </div>
</td> </td>
@ -430,12 +431,27 @@ function openNearPosts(posts) {
markerLayers.addTo(map); markerLayers.addTo(map);
let markersBounds = []; let markersBounds = [];
let coords = [];
posts.posts.forEach((post) => { posts.posts.forEach((post) => {
let marker = L.marker(L.latLng(post.geo.lat, post.geo.lng)).addTo(map); if (coords.includes(`${post.geo.lat} ${post.geo.lng}`)) {
marker.bindPopup(getPostPopup(post)); markerLayers.getLayers().forEach((marker) => {
markerLayers.addLayer(marker); if (marker.getLatLng().lat === post.geo.lat && marker.getLatLng().lng === post.geo.lng) {
markersBounds.push(marker.getLatLng()); let content = marker.getPopup()._content += getPostPopup(post);
if (!content.startsWith(`<div style="max-height: 300px; overflow-y: auto;">`))
content = `<div style="max-height: 300px; overflow-y: auto;">${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); let bounds = L.latLngBounds(markersBounds);