root/g-map/files/lib/page/MapAjaxPage.class.php @ 1235

Revision 1235, 2.8 kB (checked in by d0nut, 3 years ago)

TODO: let javascript control the bounding boxes

Line 
1<?php
2// wcf imports
3require_once(WCF_DIR.'lib/page/AbstractPage.class.php');
4
5// map imports
6require_once(WCF_DIR.'lib/data/gmap/GmapCluster.class.php');
7
8/**
9 * Returns the AJAX Content for the Gooogle Map
10 *
11 * @package     de.gmap.wcf.data.page
12 * @author      Torben Brodt
13 * @license     GNU General Public License <http://opensource.org/licenses/gpl-3.0.html>
14 */
15class MapAjaxPage extends AbstractPage {
16        protected $zoom = 0;
17        protected $distance = 35;
18        protected $bounds= array();
19        protected $initialized = false;
20       
21        protected $markers = array();
22       
23        /**
24         * @see Page::readData()
25         */
26        public function readParameters() {
27                parent::readParameters();
28               
29                $this->zoom = max(min(21, isset($_GET['zoom']) ? $_GET['zoom'] : 0), 0);
30               
31                // ((50.08930948264218, 10.298652648925781), (50.14434619645057, 10.506362915039062))
32                if(isset($_GET['bounds'])) {
33                        if(preg_match('/^\(\((-?\d+\.?\d*), (-?\d+\.?\d*)\), \((-?\d+\.?\d*), (-?\d+\.?\d*)\)\)$/', $_GET['bounds'], $match)) {
34                                $this->bounds = array(
35                                        array(
36                                                'lat' => $match[1],
37                                                'lon' => $match[2]
38                                        ),
39                                        array(
40                                                'lat' => $match[3],
41                                                'lon' => $match[4]
42                                        ),
43                                );
44
45                                // extra 30%
46                                // TODO: let javascript control the bounding box
47                                $this->bounds[0]['lat'] -= abs($this->bounds[0]['lat'] - $this->bounds[1]['lat']) * 0.3;
48                                $this->bounds[1]['lat'] += abs($this->bounds[0]['lat'] - $this->bounds[1]['lat']) * 0.3;
49                                $this->bounds[0]['lon'] -= abs($this->bounds[0]['lon'] - $this->bounds[1]['lon']) * 0.3;
50                                $this->bounds[1]['lon'] += abs($this->bounds[0]['lon'] - $this->bounds[1]['lon']) * 0.3;
51                        }
52                }
53               
54                // just get bounds
55                $this->initialized = isset($_GET['initialized']) && $_GET['initialized'];
56               
57                // load content
58                $this->content = isset($_GET['content']) && $_GET['content'];
59        }
60       
61        /**
62         * @see Page::readData()
63         */
64        public function readData() {
65                parent::readData();
66               
67                $markers = array();
68
69                $sql = 'SELECT          X(pt) AS lon,
70                                        Y(pt) AS lat
71                        FROM            wcf'.WCF_N.'_gmap_user
72                        WHERE           1';
73                       
74                if($this->bounds) {
75                        $sql .= ' AND X(pt) BETWEEN '.floatval($this->bounds[0]['lon']).' AND '.floatval($this->bounds[1]['lon']).' ';
76                        $sql .= ' AND Y(pt) BETWEEN '.floatval($this->bounds[0]['lat']).' AND '.floatval($this->bounds[1]['lat']).' ';
77                }
78                       
79                if(!$this->initialized) {
80                        $sql = 'SELECT  AVG(lon) AS lon,
81                                        AVG(lat) AS lat
82                                FROM (
83                                        '.$sql.'
84                                ) x';
85                }
86
87                $result = WCF::getDB()->sendQuery($sql);
88                while ($row = WCF::getDB()->fetchArray($result)) {
89                        $markers[] = $row;
90                }
91               
92                $cluster = new GmapCluster($this->distance, $this->zoom);
93                $this->markers = $cluster->getMarkers($markers);
94        }
95
96        /**
97         * @see Page::show()
98         */
99        public function show() {
100                parent::show();
101
102                echo json_encode($this->markers);
103        }
104}
105?>
Note: See TracBrowser for help on using the browser.