Merged in feature/MAPG-3-show-the-place-from-a-database (pull request #1)

Feature/MAPG-3 show the place from a database
This commit is contained in:
Bence Pőcze 2020-05-17 21:42:10 +00:00
commit 6ae34c8475
11 changed files with 134 additions and 42 deletions

View File

@ -1,2 +1,6 @@
DEV=true
DB_HOST=mariadb
DB_USER=mapguesser
DB_PASSWORD=mapguesser
DB_NAME=mapguesser
GOOGLE_MAPS_JS_API_KEY=your_google_maps_js_api_key

27
install/db.sql Normal file
View File

@ -0,0 +1,27 @@
SET NAMES utf8mb4;
SET foreign_key_checks = 0;
DROP TABLE IF EXISTS `maps`;
CREATE TABLE `maps` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`description` text NOT NULL,
`bound_south_lat` decimal(8,6) NOT NULL,
`bound_west_lng` decimal(9,6) NOT NULL,
`bound_north_lat` decimal(8,6) NOT NULL,
`bound_east_lng` decimal(9,6) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
DROP TABLE IF EXISTS `places`;
CREATE TABLE `places` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`map_id` int(10) unsigned NOT NULL,
`lat` decimal(8,6) NOT NULL,
`lng` decimal(9,6) NOT NULL,
PRIMARY KEY (`id`),
KEY `map_id` (`map_id`),
CONSTRAINT `places_map_id` FOREIGN KEY (`map_id`) REFERENCES `maps` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

View File

@ -2,7 +2,9 @@
require 'vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
const ROOT = __DIR__;
$dotenv = Dotenv\Dotenv::createImmutable(ROOT);
$dotenv->load();
if (!empty($_ENV['DEV'])) {

5
public/.htaccess Normal file
View File

@ -0,0 +1,5 @@
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

View File

@ -2,39 +2,15 @@
require '../main.php';
// demo position
$realPosition = new MapGuesser\Geo\Position(47.85239, 13.35101);
// very basic routing
$url = $_SERVER['REQUEST_URI'];
switch($url) {
case '/':
$controller = new MapGuesser\Controller\GuessController();
break;
default:
echo 'Error 404';
die;
}
// demo bounds
$bounds = new MapGuesser\Geo\Bounds($realPosition);
$bounds->extend(new MapGuesser\Geo\Position(48.07683,7.35758));
$bounds->extend(new MapGuesser\Geo\Position(47.57496, 19.08077));
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>MapGuesser</title>
<link rel="stylesheet" type="text/css" href="static/css/mapguesser.css">
</head>
<body>
<div id="panorama"></div>
<div id="guess">
<div id="guessMap"></div>
<div id="guessButtonContainer">
<button id="guessButton" disabled>Guess</button>
</div>
</div>
<script>
var realPosition = <?= $realPosition->toJson() ?>;
var guessMapBounds = <?= $bounds->toJson() ?>;
</script>
<script src="static/js/mapguesser.js" async defer></script>
<script src="https://maps.googleapis.com/maps/api/js?key=<?= $_ENV['GOOGLE_MAPS_JS_API_KEY'] ?>&callback=initialize" async defer></script>
</body>
</html>
echo $controller->render();

View File

@ -53,10 +53,6 @@ var googleLink;
function initialize() {
panorama = new google.maps.StreetViewPanorama(document.getElementById('panorama'), {
position: realPosition,
pov: {
heading: 34,
pitch: 10
},
disableDefaultUI: true,
linksControl: true,
showRoadLabels: false

View File

@ -0,0 +1,24 @@
<?php namespace MapGuesser\Controller;
abstract class BaseController
{
protected string $view;
protected array $variables = [];
public function render() : string
{
$this->operate();
extract($this->variables);
ob_start();
require ROOT . '/views/' . $this->view . '.php';
$content = ob_get_contents();
ob_end_clean();
return $content;
}
abstract protected function operate() : void;
}

View File

@ -0,0 +1,35 @@
<?php namespace MapGuesser\Controller;
use MapGuesser\Util\Geo\Bounds;
use MapGuesser\Util\Geo\Position;
use mysqli;
class GuessController extends BaseController
{
protected string $view = 'guess';
protected function operate() : void
{
$mysql = new mysqli($_ENV['DB_HOST'], $_ENV['DB_USER'], $_ENV['DB_PASSWORD'], $_ENV['DB_NAME']);
// demo map
$mapId = 1;
// using RAND() for the time being, could be changed in the future
$stmt = $mysql->prepare('SELECT lat, lng FROM places WHERE map_id=? ORDER BY RAND() LIMIT 1');
$stmt->bind_param("i", $mapId);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$realPosition = new Position($row['lat'], $row['lng']);
// demo bounds
$bounds = new Bounds($realPosition);
$bounds->extend(new Position(48.07683,7.35758));
$bounds->extend(new Position(47.57496, 19.08077));
$this->variables = compact('realPosition', 'bounds');
}
}

View File

@ -1,4 +1,4 @@
<?php namespace MapGuesser\Geo;
<?php namespace MapGuesser\Util\Geo;
class Bounds
{

View File

@ -1,4 +1,4 @@
<?php namespace MapGuesser\Geo;
<?php namespace MapGuesser\Util\Geo;
class Position
{

23
views/guess.php Normal file
View File

@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>MapGuesser</title>
<link rel="stylesheet" type="text/css" href="static/css/mapguesser.css">
</head>
<body>
<div id="panorama"></div>
<div id="guess">
<div id="guessMap"></div>
<div id="guessButtonContainer">
<button id="guessButton" disabled>Guess</button>
</div>
</div>
<script>
var realPosition = <?= $realPosition->toJson() ?>;
var guessMapBounds = <?= $bounds->toJson() ?>;
</script>
<script src="static/js/mapguesser.js" async defer></script>
<script src="https://maps.googleapis.com/maps/api/js?key=<?= $_ENV['GOOGLE_MAPS_JS_API_KEY'] ?>&callback=initialize" async defer></script>
</body>
</html>