@@ -26,7 +26,7 @@ public static function lngToXTile(float $lon, int $zoom): array
2626 $ tile = \floor ($ x );
2727 return [
2828 'id ' => $ tile ,
29- 'position ' => 256 * ($ x - $ tile )
29+ 'position ' => \round ( 256 * ($ x - $ tile) )
3030 ];
3131 }
3232
@@ -42,30 +42,32 @@ public static function latToYTile(float $lat, int $zoom): array
4242 $ tile = \floor ($ y );
4343 return [
4444 'id ' => $ tile ,
45- 'position ' => 256 * ($ y - $ tile )
45+ 'position ' => \round ( 256 * ($ y - $ tile) )
4646 ];
4747 }
4848
4949 /**
5050 * Convert horizontal OpenStreetMap tile number ad zoom to longitude.
51- * @param int $x Horizontal OpenStreetMap tile id
51+ * @param int $id Horizontal OpenStreetMap tile id
52+ * @param int $position Horizontal pixel position on tile
5253 * @param int $zoom Zoom
5354 * @return float Longitude of the given OpenStreetMap tile id and zoom
5455 */
55- public static function xTileToLng (int $ x , int $ zoom ): float
56+ public static function xTileToLng (int $ id , int $ position , int $ zoom ): float
5657 {
57- return $ x / \pow (2 , $ zoom ) * 360 - 180 ;
58+ return ( $ id + $ position / 256 ) / \pow (2 , $ zoom ) * 360 - 180 ;
5859 }
5960
6061 /**
6162 * Convert vertical OpenStreetMap tile number and zoom to latitude.
62- * @param int $y Vertical OpenStreetMap tile id
63+ * @param int $id Vertical OpenStreetMap tile id
64+ * @param int $position Vertical pixel position on tile
6365 * @param int $zoom Zoom
6466 * @return float Latitude of the given OpenStreetMap tile id and zoom
6567 */
66- public static function yTileToLat (int $ y , int $ zoom ): float
68+ public static function yTileToLat (int $ id , int $ position , int $ zoom ): float
6769 {
68- return \rad2deg (\atan (\sinh (M_PI * (1 - 2 * $ y / \pow (2 , $ zoom )))));
70+ return \rad2deg (\atan (\sinh (M_PI * (1 - 2 * ( $ id + $ position / 256 ) / \pow (2 , $ zoom )))));
6971 }
7072
7173
@@ -76,11 +78,11 @@ public static function yTileToLat(int $y, int $zoom): float
7678 * @param int $zoom Zoom
7779 * @return float latitude
7880 */
79- public static function tilePxToLat (float $ pxPosition , int $ tile , int $ zoom ): float
81+ /* public static function tilePxToLat(float $pxPosition, int $tile, int $zoom): float
8082 {
8183 $tileLat = static::yTileToLat($tile, $zoom);
8284 return $tileLat - \abs(($tileLat - static::yTileToLat($tile + 1, $zoom)) * $pxPosition / 256);
83- }
85+ }*/
8486
8587 /**
8688 * Convert pixel position from left of the tile to longitude.
@@ -89,11 +91,11 @@ public static function tilePxToLat(float $pxPosition, int $tile, int $zoom): flo
8991 * @param int $zoom Zoom
9092 * @return float longitude
9193 */
92- public static function tilePxToLng (float $ pxPosition , int $ tile , int $ zoom ): float
94+ /* public static function tilePxToLng(float $pxPosition, int $tile, int $zoom): float
9395 {
9496 $tileLng = static::xTileToLng($tile, $zoom);
9597 return $tileLng + \abs(($tileLng - static::xTileToLng($tile + 1, $zoom)) * $pxPosition / 256);
96- }
98+ }*/
9799
98100 /**
99101 * @var int zoom
@@ -169,20 +171,20 @@ public function __construct(LatLng $centerMap, int $zoom, XY $outputSize)
169171 );
170172
171173 $ this ->latLngTopLeft = new LatLng (
172- static ::tilePxToLat ($ this ->mapCropTopLeft ->getY (), $ this ->tileTopLeft ->getY (), $ zoom ),
173- static ::tilePxToLng ($ this ->mapCropTopLeft ->getX (), $ this ->tileTopLeft ->getX (), $ zoom )
174+ static ::yTileToLat ($ this ->tileTopLeft ->getY (), $ this ->mapCropTopLeft ->getY (), $ zoom ),
175+ static ::xTileToLng ($ this ->tileTopLeft ->getX (), $ this ->mapCropTopLeft ->getX (), $ zoom )
174176 );
175177 $ this ->latLngTopRight = new LatLng (
176- static ::tilePxToLat ($ this ->mapCropTopLeft ->getY (), $ this ->tileTopLeft ->getY (), $ zoom ),
177- static ::tilePxToLng ( 256 - $ this ->mapCropBottomRight ->getX (), $ this ->tileBottomRight ->getX (), $ zoom )
178+ static ::yTileToLat ($ this ->tileTopLeft ->getY (), $ this ->mapCropTopLeft ->getY (), $ zoom ),
179+ static ::xTileToLng ( $ this ->tileBottomRight ->getX (), 256 - $ this ->mapCropBottomRight ->getX (), $ zoom )
178180 );
179181 $ this ->latLngBottomLeft = new LatLng (
180- static ::tilePxToLat ( 256 - $ this ->mapCropBottomRight ->getY (), $ this ->tileBottomRight ->getY (), $ zoom ),
181- static ::tilePxToLng ($ this ->mapCropTopLeft ->getX (), $ this ->tileTopLeft ->getX (), $ zoom )
182+ static ::yTileToLat ( $ this ->tileBottomRight ->getY (), 256 - $ this ->mapCropBottomRight ->getY (), $ zoom ),
183+ static ::xTileToLng ($ this ->tileTopLeft ->getX (), $ this ->mapCropTopLeft ->getX (), $ zoom )
182184 );
183185 $ this ->latLngBottomRight = new LatLng (
184- static ::tilePxToLat ( 256 - $ this ->mapCropBottomRight ->getY (), $ this ->tileBottomRight ->getY (), $ zoom ),
185- static ::tilePxToLng ( 256 - $ this ->mapCropBottomRight ->getX (), $ this ->tileBottomRight ->getX (), $ zoom )
186+ static ::yTileToLat ( $ this ->tileBottomRight ->getY (), 256 - $ this ->mapCropBottomRight ->getY (), $ zoom ),
187+ static ::xTileToLng ( $ this ->tileBottomRight ->getX (), 256 - $ this ->mapCropBottomRight ->getX (), $ zoom )
186188 );
187189 }
188190
0 commit comments