mark own positions in tiles of a slippy map

Hi Community,

while playing with the osm i start to code an php script that loads all necessary tiles of an slippymap. Now i got a Problem to draw Locationspoints (mark own Position). I get the the Position from my Android Smartphone that uses GPS. A own Logger app writes lat and lon Data in a CSV File.

For Example:

Step 1:
I load the tile in Zoomlevel 16 of lat=50.31142830848694 and lon=7.553218603134155. I get the Tilenumber with

$xtile = floor((($lon + 180) / 360) * pow(2, $zoom));
$ytile = floor((1 - log(tan(deg2rad($lat)) + 1 / cos(deg2rad($lat))) / pi()) /2 * pow(2, $zoom));

Step 2: (Here i got my Problem)
Now a want to mark the Point of lat=50.31142830848694 and lon=7.553218603134155 in the loaded Tile of the Slippymap.
I do it like this:

$n = pow(2, $zoom);
$lon_deg = $xtile / $n * 360.0 - 180.0;
$lat_deg = rad2deg(atan(sinh(pi() * (1 - 2 * $ytile / $n))));

$lat_diff = ($lat-$lat_deg);    
$lon_diff = ($lon-$lon_deg);

// 1Pixel = 2.387 Metre in Zoomlevel 16
$scaleToPixel = 2.387;
$x = intval(abs($lat * 6378137) / $scaleToPixel);
$y = intval(abs($lon * 6378137) / $scaleToPixel);                
imagefilledellipse($image, $y, $x, 2, 2, $color);

But the drawed Points arent exactly on the Position lat=50.31142830848694 and lon=7.553218603134155.

Here you can see it:

I think that happens because my calculation is bad…

If anyone can help me it can save the Day :wink:

My answer is here: