LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Blogs > spirov92
User Name
Password

Notices


Rate this Entry

GMaps API tile server

Posted 01-28-2009 at 12:59 PM by spirov92
Updated 01-28-2009 at 01:03 PM by spirov92

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.
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);

?>
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.
Posted in Uncategorized
Views 4124 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 01:02 PM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration