@@ -33,7 +33,7 @@ public static function lngToXTile(float $lon, int $zoom): int
3333 */
3434 public static function latToYTile (float $ lat , int $ zoom ): int
3535 {
36- return floor ((1 - \log (\tan (\deg2rad ($ lat )) + 1 / \cos (\deg2rad ($ lat ))) / M_PI ) / 2 * \pow (2 , $ zoom ));
36+ return \ floor ((1 - \log (\tan (\deg2rad ($ lat )) + 1 / \cos (\deg2rad ($ lat ))) / M_PI ) / 2 * \pow (2 , $ zoom ));
3737 }
3838
3939 /**
@@ -48,7 +48,7 @@ public static function xTileToLng(int $x, int $zoom): float
4848 }
4949
5050 /**
51- * Convert vertical OpenStreetMap tile number ad zoom to latitude.
51+ * Convert vertical OpenStreetMap tile number and zoom to latitude.
5252 * @param int $y Vertical OpenStreetMap tile id
5353 * @param int $zoom Zoom
5454 * @return float Latitude of the given OpenStreetMap tile id and zoom
@@ -141,24 +141,23 @@ public function getBoundingBox(): BoundingBox
141141 protected function getMapImage (): Image
142142 {
143143 $ bbox = $ this ->boundingBox ;
144+ $ yTile = static ::latToYTile ($ bbox ->getBottomLeft ()->getLat (), $ this ->zoom );
144145 $ xTile = static ::lngToXTile ($ bbox ->getBottomLeft ()->getLng (), $ this ->zoom );
145- $ yTile = static ::latToYTile ($ bbox ->getTopRight ()->getLat (), $ this ->zoom );
146146 $ startPos = $ bbox ->convertLatLngToPxPosition (new LatLng (
147147 static ::yTileToLat ($ yTile , $ this ->zoom ),
148148 static ::xTileToLng ($ xTile , $ this ->zoom )
149149 ));
150150
151151 $ image = Image::newCanvas ($ bbox ->getOutputPxSize ()->getX (), $ bbox ->getOutputPxSize ()->getY ());
152152
153- $ tmpYTile = $ yTile ;
154- for ($ y = $ startPos ->getY (); $ y < $ bbox ->getOutputPxSize ()->getY (); $ y += 256 ) {
153+ for ($ y = $ startPos ->getY (); $ y > -255 ; $ y -= 256 ) {
155154 $ tmpXTile = $ xTile ;
156155 for ($ x = $ startPos ->getX (); $ x < $ bbox ->getOutputPxSize ()->getX (); $ x += 256 ) {
157- $ i = Image::fromCurl ('https://tile.openstreetmap.org/ ' . $ this ->zoom . '/ ' . $ tmpXTile . '/ ' . $ tmpYTile . '.png ' );
156+ $ i = Image::fromCurl ('https://tile.openstreetmap.org/ ' . $ this ->zoom . '/ ' . $ tmpXTile . '/ ' . $ yTile . '.png ' );
158157 $ image ->pasteOn ($ i , $ x , $ y );
159158 ++$ tmpXTile ;
160159 }
161- ++ $ tmpYTile ;
160+ -- $ yTile ;
162161 }
163162
164163 return $ image ;
0 commit comments