public
Description: API to help working with Google Static Maps.
Homepage: http://www.appelsiini.net/projects/php_google_maps/basic.html
Clone URL: git://github.com/tuupola/php_google_maps.git
php_google_maps / Google / Maps / Bounds.php
100644 197 lines (170 sloc) 5.689 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
 
/*
* Google_Maps_Bounds
*
* Copyright (c) 2008 Mika Tuupola
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Project home:
* http://www.appelsiini.net/projects/google_maps
*
* Revision: $Id$
*
*/
 
require_once 'Google/Maps/Overload.php';
require_once 'Google/Maps/Coordinate.php';
require_once 'Google/Maps/Point.php';
 
class Google_Maps_Bounds extends Google_Maps_Overload {
 
    protected $min_lon;
    protected $min_lat;
    protected $max_lon;
    protected $max_lat;
    
    /**
* Class constructor.
*
* @param array which can be mix of Google_Maps_Coordinate|Point objects.
* @return object
*/
    
    public function __construct($location_list) {
 
        /* Make sure everything is a coordinate. */
        $coordinate_list = array();
        foreach ($location_list as $location) {
            if ('Google_Maps_Point' == get_class($location)) {
                $coordinate_list[] = $location->toCoordinate();
            } else {
                $coordinate_list[] = $location;
            }
        }
 
        $coordinate = array_pop($coordinate_list);
        $this->setMinLon($coordinate->getLon());
        $this->setMinLat($coordinate->getLat());
        $this->setMaxLon($coordinate->getLon());
        $this->setMaxLat($coordinate->getLat());
 
        foreach ($coordinate_list as $coordinate) {
            if ($coordinate->getLon() < $this->getMinLon()) {
                $this->setMinLon($coordinate->getLon());
            }
            if ($coordinate->getLon() > $this->getMaxLon()) {
                $this->setMaxLon($coordinate->getLon());
            }
            if ($coordinate->getLat() < $this->getMinLat()) {
                $this->setMinLat($coordinate->getLat());
            }
            if ($coordinate->getLat() > $this->getMaxLat()) {
                $this->setMaxLat($coordinate->getLat());
            }
        }
        
    }
 
    /**
* Return north-west corner of bounds.
*
* @return mixed Google_Maps_Coordinate or Google_Maps_Point
*/
          
    public function getNorthWest($type='') {
        $lat = $this->getMaxLat();
        $lon = $this->getMinLon();
        $retval = new Google_Maps_Coordinate($lat, $lon);
        if ('point' == $type) {
            $retval = $retval->toPoint();
        }
        return $retval;
    }
 
    /**
* Return north-east corner of bounds.
*
* @return mixed Google_Maps_Coordinate or Google_Maps_Point
*/
      
    public function getNorthEast($type='') {
        $lat = $this->getMaxLat();
        $lon = $this->getMaxLon();
        $retval = new Google_Maps_Coordinate($lat, $lon);
        if ('point' == $type) {
            $retval = $retval->toPoint();
        }
        return $retval;
    }
    
    /**
* Return souts-east corner of bounds.
*
* @return mixed Google_Maps_Coordinate or Google_Maps_Point
*/
      
    public function getSouthEast($type='') {
        $lat = $this->getMinLat();
        $lon = $this->getMaxLon();
        $retval = new Google_Maps_Coordinate($lat, $lon);
        if ('point' == $type) {
            $retval = $retval->toPoint();
        }
        return $retval;
    }
 
    /**
* Return south-west corner of bounds.
*
* @return mixed Google_Maps_Coordinate or Google_Maps_Point
*/
      
    public function getSouthWest($type='') {
        $lat = $this->getMinLat();
        $lon = $this->getMinLon();
        $retval = new Google_Maps_Coordinate($lat, $lon);
        if ('point' == $type) {
            $retval = $retval->toPoint();
        }
        return $retval;
    }
 
    /**
* Check if given coordinate, point or bounds is inside bounds
*
* @return boolean
*/
 
    public function contains($bounds_or_location) {
        if ($bounds_or_location instanceof Google_Maps_Bounds) {
            $retval = $this->containsBounds($bounds_or_location);
        } else {
            $retval = $this->containsLocation($bounds_or_location);
        }
        return $retval;
    }
 
    /**
* Check if given coordinate or point is inside bounds
*
* @return boolean
*/
 
    public function containsLocation(Google_Maps_Location $location) {
        $retval = false;
        $coordinate = $location->toCoordinate();
        if ($coordinate->getLon() < $this->getMaxLon() && $coordinate->getLon() > $this->getMinLon() &&
            $coordinate->getLat() < $this->getMaxLat() && $coordinate->getLat() > $this->getMinLat()) {
                $retval = true;
        }
        return $retval;
    }
    
    /**
* Check if given bounds is inside bounds
*
* @return boolean
*/
    
    public function containsBounds(Google_Maps_Bounds $bounds) {
        $retval = false;
        if ($this->containsLocation($bounds->getNorthEast()) &&
            $this->containsLocation($bounds->getSouthWest())) {
              $retval = true;
        }
        return $retval;
    }
 
    /**
* Returns array of path objects which can be used for drawing
* borders of current bounds object into the static map. Can be
* used for debugging.
*
* @return array of Google_Maps_Path
*/
    
    public function getPath() {
        return array(new Google_Maps_Path($this->getNorthWest()),
                     new Google_Maps_Path($this->getNorthEast()),
                     new Google_Maps_Path($this->getSouthEast()),
                     new Google_Maps_Path($this->getSouthWest()),
                     new Google_Maps_Path($this->getNorthWest()));
    }
        
}