GMaps API tile server
Tags google, google maps, php
So, I've been messing around with the GMaps API, trying to create my custom map type. It took me a lot of time, so I decided to share the code I ended up with.
This script serves content to the Google Maps API. It divides each requested tile into 8x8=64 squares and for each one displays the highest-rated picture that belongs to it.
The actual site is right now on my localhost.(It's only available when I'm at home)
Hope someone finds this useful!
p.s. This is my first post on the blog.I'll be publishing some more PHP code, and some python too.
This script serves content to the Google Maps API. It divides each requested tile into 8x8=64 squares and for each one displays the highest-rated picture that belongs to it.
PHP Code:
<?
//this script should serve content for the gmaps api
//we're echo-ing out an image, so we should show that
header('Content-Type:image/png');
// lets include some stuff
require('include/functions/__autoload.php');
require('include/mysql_configure.php');
//deg size of the cell on x and y
$cell_size_x=360/pow(2,$_GET['z']);
$cell_size_y=$cell_size_x/2;
$tiles=pow(2,$_GET['z']);
$img=imagecreatefrompng('images/test.png');//the base image
for($row=0;$row<8;$row++){
for($col=0;$col<8;$col++){
//the for loops follow the normal
//coordinates schema, but the geographical
//coordinates are inverted vertically
//and start from the center
$exp = ($_GET['y']*256+($row)*256/8-256*pow(2,$_GET['z']-1))/(-256*pow(2,$_GET['z'])/(2*pi()));
$north=(2*atan(exp($exp))-(pi()/2))/(pi()/180);
$exp=($_GET['y']*256+($row+1)*256/8-256*pow(2,$_GET['z']-1))/(-256*pow(2,$_GET['z'])/(2*pi()));
$south=(2*atan(exp($exp))-(pi()/2))/(pi()/180);
$west=$_GET['x']*$cell_size_x+($col)*$cell_size_x/8-180;
$east=$west+$cell_size_x/8;
$q=$sql->q("SELECT photos.id FROM photos LEFT JOIN photos_votes ON photos.id=photos_votes.about WHERE
photos.lat>=$south AND photos.lat<$north AND
photos.lng>=$west and photos.lng<$east GROUP BY photos.id
ORDER BY AVG(photos_votes.vote) DESC LIMIT 1");
if(!$q)die(mysql_error());
if(mysql_num_rows($q)){
$q=mysql_fetch_array($q);
$small=imagecreatefrompng('images/small/'.$q['id'].'.png');
imagecopy($img,$small,
$col*32,
$row*32,
0,0,32,32);
}
}
}
//fix transparency and echo out the image
imagecolortransparent($img,imagecolorallocate($img,255,255,255));
imagepng($img);
?>
Hope someone finds this useful!
p.s. This is my first post on the blog.I'll be publishing some more PHP code, and some python too.
Total Comments 0