diff --git a/lib/Location.class.php b/lib/Location.class.php index 7b11081..a6476da 100644 --- a/lib/Location.class.php +++ b/lib/Location.class.php @@ -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 () { diff --git a/mytown.php b/mytown.php index 49f66e8..1bb2a91 100644 --- a/mytown.php +++ b/mytown.php @@ -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 ' diff --git a/tests/osm-search.php b/tests/osm-search.php new file mode 100644 index 0000000..ae845f8 --- /dev/null +++ b/tests/osm-search.php @@ -0,0 +1,19 @@ +getPosition(), $martinique->getPosition());