FIX: certains événements en trop apparaissaient dans la liste locale

This commit is contained in:
Boris Paing 2020-12-12 17:36:41 +01:00
parent fce3fa5763
commit eed476a20b
3 changed files with 89 additions and 36 deletions

View File

@ -16,14 +16,17 @@ class Location {
private $postCode;
private $successfulQuery;
public function __construct () {
}
public function createFromAddress ($searchQuery) {
public function fetchOpenStreetMap ($searchQuery) {
$json = NULL;
$streamContext = stream_context_create(
array(
"http" => array(
@ -32,9 +35,41 @@ class Location {
)
);
$url = sprintf(Location::API_URL, $searchQuery);
$json = file_get_contents($url, false, $streamContext);
$results = json_decode($json);
$url = sprintf(Location::API_URL, urlencode($searchQuery));
$json = @file_get_contents($url, false, $streamContext);
if (!empty($json)) {
$json = json_decode($json);
}
return $json;
}
public function createFromAddress ($searchTerms) {
if (is_array($searchTerms)) {
while (!empty($searchTerms)) {
$searchQuery = implode(' ', $searchTerms);
$results = Location::fetchOpenStreetMap($searchQuery);
if (empty($json)) {
$searchTerms = array_slice($searchTerms, 0, -1);
} else {
break;
}
}
} else {
$searchQuery = $searchTerms;
$results = Location::fetchOpenStreetMap($searchQuery);
}
if (isset($results[0])) {
@ -43,6 +78,7 @@ class Location {
$loc = new Location();
$loc->setPosition($firstResult->lat, $firstResult->lon);
$loc->successfulQuery = $searchQuery;
return $loc;
@ -63,6 +99,11 @@ class Location {
return [$this->lat, $this->lon];
}
public function getSuccessfulQuery () {
return $this->successfulQuery;
}
public function getLat () {

View File

@ -20,7 +20,10 @@ if (isset($_GET['location'])) {
$_SESSION['location'] = htmlspecialchars($_GET['location']);
}
} elseif (isset($_SESSION['lat'], $_SESSION['lon'])) {
}
if (isset($_SESSION['lat'], $_SESSION['lon'])) {
$userPos = new Location();
$userPos->setPosition($_SESSION['lat'], $_SESSION['lon']);
@ -82,42 +85,24 @@ $limit = 20;
$todayIso = new DateTime();
foreach($x->topic_list->topics as $entry) {
$eventLoc = NULL;
$eventDate = new DateTime($entry->event->start);
if ($eventDate >= $todayIso) {
if (isset($entry->location->geo_location->lat, $entry->location->geo_location->lon)) {
$loc = new Location();
$loc->setPosition($entry->location->geo_location->lat, $entry->location->geo_location->lon);
$eventLoc = new Location();
$eventLoc->setPosition($entry->location->geo_location->lat, $entry->location->geo_location->lon);
} else {
$searchTerms = [];
if (isset($entry->location->street)) {
$searchTerms[] = $entry->location->street;
}
if (isset($entry->location->geo_location->country)) {
if (isset($entry->location->name)) {
$searchTerms[] = $entry->location->name;
}
if (isset($entry->location->geo_location->address)) {
$searchTerms[] = $entry->location->geo_location->address;
}
if (isset($entry->location->geo_location->city)) {
$searchTerms[] = $entry->location->geo_location->city;
}
if (isset($entry->location->geo_location->postalcode)) {
$searchTerms[] = $entry->location->geo_location->postalcode;
$searchTerms[] = $entry->location->geo_location->country;
}
if (isset($entry->location->geo_location->state)) {
@ -125,21 +110,29 @@ foreach($x->topic_list->topics as $entry) {
$searchTerms[] = $entry->location->geo_location->state;
}
if (isset($entry->location->geo_location->country)) {
if (isset($entry->location->geo_location->postalcode)) {
$searchTerms[] = $entry->location->geo_location->country;
$searchTerms[] = $entry->location->geo_location->postalcode;
}
if (isset($entry->location->geo_location->city)) {
$searchTerms[] = $entry->location->geo_location->city;
}
if (isset($entry->location->geo_location->address)) {
$searchTerms[] = $entry->location->geo_location->address;
}
if (!empty($searchTerms)) {
$q = implode(' ', $searchTerms);
$loc = Location::createFromAddress($q);
$eventLoc = Location::createFromAddress($searchTerms);
}
}
if (isset($loc) and $loc and (($dist = Location::geoDist($loc->getPosition(), $userPos->getPosition())) < EVENT_RADIUS)) {
if (isset($eventLoc) and $eventLoc and (($dist = Location::geoDist($eventLoc->getPosition(), $userPos->getPosition())) <= EVENT_RADIUS)) {
echo '

19
tests/osm-search.php Normal file
View File

@ -0,0 +1,19 @@
<?php
require_once('../lib/Location.class.php');
$point1 = [47.62862971426153, -2.6431060558159647];
$a = 'Rennes';
$b = ['Martinique', 'Domaine de Tivoli'];
$rennes = Location::createFromAddress($a);
if ($rennes === false) {
die('pas trouvé "'. $a .'"');
}
$martinique = Location::createFromAddress($b);
if ($martinique === false) {
die('pas trouvé "'. implode(' ', $b) .'');
}
echo Location::geoDist($rennes->getPosition(), $martinique->getPosition());