Extract static methods.

This commit is contained in:
Malte Hübner 2017-05-26 16:21:41 +02:00
parent 5a7bdc21b1
commit 11e15874df
2 changed files with 25 additions and 18 deletions

View File

@ -135,20 +135,11 @@ class Printer
return $this;
}
public function lonToTile($long, $zoom)
{
return (($long + 180) / 360) * pow(2, $zoom);
}
public function latToTile($lat, $zoom)
{
return (1 - log(tan($lat * pi() / 180) + 1 / cos($lat * pi() / 180)) / pi()) / 2 * pow(2, $zoom);
}
public function initCoords()
{
$this->centerX = $this->lonToTile($this->lon, $this->zoom);
$this->centerY = $this->latToTile($this->lat, $this->zoom);
$this->centerX = Util::lonToTile($this->lon, $this->zoom);
$this->centerY = Util::latToTile($this->lat, $this->zoom);
$this->offsetX = floor((floor($this->centerX) - $this->centerX) * $this->tileSize);
$this->offsetY = floor((floor($this->centerY) - $this->centerY) * $this->tileSize);
}
@ -236,8 +227,8 @@ class Printer
}
// calc position
$destX = floor(($this->width / 2) - $this->tileSize * ($this->centerX - $this->lonToTile($markerLon, $this->zoom)));
$destY = floor(($this->height / 2) - $this->tileSize * ($this->centerY - $this->latToTile($markerLat, $this->zoom)));
$destX = floor(($this->width / 2) - $this->tileSize * ($this->centerX - Util::lonToTile($markerLon, $this->zoom)));
$destY = floor(($this->height / 2) - $this->tileSize * ($this->centerY - Util::latToTile($markerLat, $this->zoom)));
// copy shadow on basemap
if ($markerShadow && $markerShadowImg) {
@ -282,14 +273,14 @@ class Printer
$sourceLongitude = array_shift($polylineList);
}
$sourceX = floor(($this->width / 2) - $this->tileSize * ($this->centerX - $this->lonToTile($sourceLongitude, $this->zoom)));
$sourceY = floor(($this->height / 2) - $this->tileSize * ($this->centerY - $this->latToTile($sourceLatitude, $this->zoom)));
$sourceX = floor(($this->width / 2) - $this->tileSize * ($this->centerX - Util::lonToTile($sourceLongitude, $this->zoom)));
$sourceY = floor(($this->height / 2) - $this->tileSize * ($this->centerY - Util::latToTile($sourceLatitude, $this->zoom)));
$destinationLatitude = array_shift($polylineList);
$destinationLongitude = array_shift($polylineList);
$destinationX = floor(($this->width / 2) - $this->tileSize * ($this->centerX - $this->lonToTile($destinationLongitude, $this->zoom)));
$destinationY = floor(($this->height / 2) - $this->tileSize * ($this->centerY - $this->latToTile($destinationLatitude, $this->zoom)));
$destinationX = floor(($this->width / 2) - $this->tileSize * ($this->centerX - Util::lonToTile($destinationLongitude, $this->zoom)));
$destinationY = floor(($this->height / 2) - $this->tileSize * ($this->centerY - Util::latToTile($destinationLatitude, $this->zoom)));
imageline($this->image , $sourceX, $sourceY , $destinationX, $destinationY, $color);

16
src/Util.php Normal file
View File

@ -0,0 +1,16 @@
<?php
namespace StaticMapLite;
class Util
{
public static function lonToTile(float $longitude, int $zoom): float
{
return (($longitude + 180) / 360) * pow(2, $zoom);
}
public static function latToTile(float $latitude, int $zoom): float
{
return (1 - log(tan($latitude * pi() / 180) + 1 / cos($latitude * pi() / 180)) / pi()) / 2 * pow(2, $zoom);
}
}