Skip to content

Commit ec5ffe4

Browse files
committed
Fix issue #5 : Wrong marker position on smaller zoom numbers
1 parent 56b4d43 commit ec5ffe4

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

src/BoundingBox.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function convertLatLngToPxPosition(LatLng $latLng): XY
8282
{
8383
return new XY(
8484
\round(($latLng->getLng() - $this->getBottomLeft()->getLng()) * $this->coef->getLng()),
85-
\round($this->outputPxSize->getY() - ($latLng->getLat() - $this->getBottomLeft()->getLat()) * $this->coef->getLat())
85+
$this->outputPxSize->getY() - \round(($latLng->getLat() - $this->getBottomLeft()->getLat()) * $this->coef->getLat())
8686
);
8787
}
8888

src/OpenStreetMap.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)