I have a question regarding Map tile server and coordinates conversion in Google Maps using the Google Maps Utility library.
My tile server accesses a database with thousands of gps coordinates (lat,lng), and for every (lat,lng) point, checks if the point is inside the geographical bounds of the tile; if it does, coordinates conversion (WGS84 -> Mercator -> X,Y offset inside the tile) and paints the corresponding pixel inside the tile, using the GoogleMapsUtility Library.
In terms of code, I do the following:
$point = GoogleMapUtility::getOffsetPixelCoords((float)$row['lat'], (float)$row['lng'], $zoom, $X, $Y);
which calls the getOffsetPixelCoords
function (and in turn the functions below) from the library:
public static function getOffsetPixelCoords($lat,$lng,$zoom, $X, $Y)
{
$pixelCoords = GoogleMapUtility::getPixelCoords($lat, $lng, $zoom);
return new Point(
$pixelCoords->x - $X * GoogleMapUtility::TILE_SIZE,
$pixelCoords->y - $Y * GoogleMapUtility::TILE_SIZE
);
}
public static function getPixelCoords($lat, $lng, $zoom)
{
$normalised = GoogleMapUtility::_toNormalisedMercatorCoords(GoogleMapUtility::_toMercatorCoords($lat, $lng));
$scale = (1 << ($zoom)) * GoogleMapUtility::TILE_SIZE;
return new Point(
(int)($normalised->x * $scale),
(int)($normalised->y * $scale)
);
}
private static function _toNormalisedMercatorCoords($point)
{
$point->x += 0.5;
$point->y = abs($point->y-0.5);
return $point;
}
Ok, now the results. For a zoom level<13 it works great, below is an example of a tile in Zoom level 11:
However, for a tile in zoom level >13, the following happens:
Which is so strange... the pixels seem to be perfectly aligned ? At first I thought it is a decimal resolution problem, but the resolution of the data is quite good (stored as double in a mysql database, for example, 35.6185989379883, 139.731994628906, and in php floats and doubles are the same thing...)
Could someone help me on how to fix this problem?
Thanks in advance...