Browse Source

initial

git-svn-id: http://svn.code.sf.net/p/staticmaplite/code@1 3c7674d3-d6c2-4d0e-87b7-95661b9333b9
pull/1/head
rac98 10 years ago
commit
76d853d1b5
8 changed files with 321 additions and 0 deletions
  1. BIN
      images/markers/lightblue1.png
  2. BIN
      images/markers/lightblue2.png
  3. BIN
      images/markers/lightblue3.png
  4. BIN
      images/markers/lightblue4.png
  5. BIN
      images/markers/lightblue5.png
  6. BIN
      images/osm_logo.png
  7. +122
    -0
      index.html
  8. +199
    -0
      staticmap.php

BIN
images/markers/lightblue1.png View File

Before After
Width: 17  |  Height: 19  |  Size: 317B

BIN
images/markers/lightblue2.png View File

Before After
Width: 17  |  Height: 19  |  Size: 417B

BIN
images/markers/lightblue3.png View File

Before After
Width: 17  |  Height: 19  |  Size: 435B

BIN
images/markers/lightblue4.png View File

Before After
Width: 17  |  Height: 19  |  Size: 387B

BIN
images/markers/lightblue5.png View File

Before After
Width: 17  |  Height: 19  |  Size: 434B

BIN
images/osm_logo.png View File

Before After
Width: 60  |  Height: 20  |  Size: 1.1KB

+ 122
- 0
index.html View File

@@ -0,0 +1,122 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de-de" lang="de-de">
<head>
<!--
CSS based on template of Dandelion wiki engine by Radomir Dopieralski who released this
template under the terms of GNU GPL. http://dandelion.sheep.art.pl/
-->
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>staticMapLite</title>
<style type="text/css">
html{font:96% sans-serif;color:#000;background:#f7f7f7;line-height:1.4;}
body{color:#333;}
#wrapper{margin:auto;width:60em;position:relative;}
#header{padding:0px 0px 7px 0px; height: 1em;}
#header h1 { float:left; width: 40%; }
#content{background:white;padding:1em;border:1px solid #e0d78a;outline:0.5em solid #fef4a4; margin:0.5em 0;padding:20px;min-height:20em;}
h1{margin-top:0px;}
h1,h2,h3,h4,h5,h6{letter-spacing:0.05em;color:#1474CA;font-weight:normal;}
h1 a:hover,h2 a:hover,h3 a:hover,h4 a:hover,h5 a:hover,h6 a:hover{text-decoration:none;}
a{color:#1474CA;text-decoration:none;}
a:visited{color:#1474CA;}
a.pending{color:#c174a0;}
a:hover{text-decoration:underline;}
a img{border:none;}
input,textarea{font-size:94%;border:1px solid #999;background:#fff;color:#666;outline:0.2em solid #eee;padding:0px;line-height:1.2;margin:0.5em;vertical-align:middle;}
textarea{display:block;margin:0.5em auto;width:100%;}
pre{outline:0.4em solid #eee;padding:0.5em;margin:0.5em;border:1px solid #e0d78a;background:#fef4a4;color:#644e22;}
img{border:1px solid #ccc;outline:0.25em solid #eee;padding:0.25em;margin:0.25em 0 0.25em 0.5em;background:#fff;}
hr{height:0;border:none;color:#fff;background:transparent;border-bottom:1px solid #ccc; margin:0.5em 0;}
#diff {outline:none;border:none;}
#diff ins{color:green;text-decoration:none;font-weight:bold;}
#diff del{color:red;text-decoration:line-through;}
#diff{background:#fff;line-height:1.25;padding:1em;white-space:pre-wrap;word-wrap:break-word; white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;width:97%;}
hr{margin:10px 0 10px 0;height:0px;overflow:hidden;border:0px;border-top:2px solid #ccc;}
.error{color:#F25A5A;font-weight:bold;}
form{display:inline;}
#contentTextarea{height:44em;}
#toc{margin:5px 0 5px 10px;padding:6px 5px 7px 0px;float:right;list-style:none;outline:0.4em solid #eee;background:#fef4a4;border:1px solid #e0d78a;}
#toc ul{list-style:none;padding:3px 0 3px 10px;}
#toc li{font-size:11px;padding-left:10px;}
#toc ul li{font-size:10px;}
#toc ul ul li{font-size:9px;}
#toc ul ul ul li{font-size:8px;}
#toc ul ul ul ul li{font-size:7px;}
.pageVersionsList{letter-spacing:0px;font-variant:normal;font-size:12px;}
#renameForm{float:left;}
.clear{clear:both;}
.tagList{padding:0.2em 0.4em 0.2em 0.4em;margin-top:0.5em;border:1px dashed #e0d78a;background:#fef4a4;color:#644e22;}
.tagCloud{float:right;width:200px;padding:0.5em;margin:1em;border:1px dashed #e0d78a;background:#fef4a4;color:#644e22;}
#fileTable{border-collapse:collapse;}
#fileTable td{border:1px solid #FEF4A4;padding:2px 6px 2px 6px;}
h2 span.par-edit, h3 span.par-edit, h4 span.par-edit, h5 span.par-edit, h6 span.par-edit {display:none;}
h2:hover span.par-edit, h3:hover span.par-edit, h4:hover span.par-edit, h5:hover span.par-edit, h6:hover span.par-edit {display:inline;font-size:x-small;}
.comment-item { border:1px solid #999;color:#666;outline:0.2em solid #eee; }
.resizeTextareaDiv { margin-top: 5px;}
a.toolbarTextareaItem { padding-right: 10px; }
a.external:after { content: "\2197";}
</style>
</head>

<body>
<div id="wrapper">
<div id="header">
</div>
<div id="content">

<div class="par-div">
<h2>
staticMapLite - simple map for your website
</h2>
<p>
<img src="staticmap.php?center=40.714728,-73.998672&zoom=14&size=865x512&maptype=mapnik" width="865" height="512" /></p>
<p>
This image was created using the following simple &lt;img> tag:
<pre>&lt;img src="staticmap.php?center=40.714728,-73.998672&amp;zoom=14&amp;size=865x512&amp;maptype=mapnik" /&gt;</pre>
</p>
</div>
<hr />
<div class="par-div">
<h3>
Place Markers
</h3>

<p>
<img src="staticmap.php?center=40.714728,-73.998672&zoom=14&size=865x512&maptype=mapnik&markers=40.702147,-74.015794,lightblue1|40.711614,-74.012318,lightblue2|40.718217,-73.998284,lightblue3" width="865" height="512" />
</p><p> Add markers by appending them to the image URL:
<pre>markers=40.702147,-74.015794,lightblue1|40.711614,-74.012318,lightblue2|40.718217,-73.998284,lightblue3</pre>
</p>
</div>
<hr />
<div class="par-div">
<h3>
Use Different Map Styles (Tile Sources)
</h3>

<p>
<div style="float:left; margin-right: 10px">
<img src="staticmap.php?center=40.714728,-73.998672&zoom=14&size=256x256&maptype=mapnik" width="256" height="256" />
<pre>maptype=mapnik</pre>
</div>
<div style="float:left; margin-right: 10px">
<img src="staticmap.php?center=40.714728,-73.998672&zoom=14&size=256x256&maptype=osmarenderer" width="256" height="256" />
<pre>maptype=osmarenderer</pre>
</div>
<div style="float:left; margin-right: 10px">
<img src="staticmap.php?center=40.714728,-73.998672&zoom=14&size=256x256&maptype=cycle" width="256" height="256" />
<pre>maptype=cycle</pre>
</div>
<br style="clear:both" />
</p>
</div>

</div>
<div id="footer">
<div style="text-align:center;padding:7px;color:#ccc">
sponsored by <a href="http://dfacts.de">dFacts Network</a>
</div>
</div>
</div>
</body>
</html>

+ 199
- 0
staticmap.php View File

@@ -0,0 +1,199 @@
<?php

/**
* staticMapLite 0.01
*
* Copyright 2009 Gerhard Koch
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Gerhard Koch <gerhard.koch AT ymail.com>
*
* USAGE:
*
* staticmap.php?center=40.714728,-73.998672&zoom=14&size=512x512&maptype=mapnik&markers=40.702147,-74.015794,blues|40.711614,-74.012318,greeng|40.718217,-73.998284,redc
*
*/

error_reporting(0);
ini_set('display_errors','off');

Class staticMapLite {

protected $tileSize = 256;
protected $tileSrcUrl = array( 'mapnik' => 'http://tile.openstreetmap.org/{Z}/{X}/{Y}.png',
'osmarenderer' => 'http://c.tah.openstreetmap.org/Tiles/tile/{Z}/{X}/{Y}.png',
'cycle' => 'http://c.andy.sandbox.cloudmade.com/tiles/cycle/{Z}/{X}/{Y}.png'
);
protected $tileDefaultSrc = 'mapnik';
protected $markerBaseDir = 'images/markers';
protected $osmLogo = 'images/osm_logo.png';

protected $useTileCache = true;
protected $tileCacheBaseDir = 'cache';
protected $zoom, $lat, $lon, $width, $height, $markers, $image, $maptype;
protected $centerX, $centerY, $offsetX, $offsetY;

public function __construct(){
$this->zoom = 0;
$this->lat = 0;
$this->lon = 0;
$this->width = 0;
$this->height = 0;
$this->markers = array();
$this->maptype = $this->tileDefaultSrc;
}
public function parseParams(){
global $_GET;
$this->zoom = $_GET['zoom']?intval($_GET['zoom']):0;
if($this->zoom>18)$this->zoom = 18;
list($this->lat,$this->lon) = split(',',$_GET['center']);
if($_GET['size']){
list($this->width, $this->height) = split('x',$_GET['size']);
}
if($_GET['markers']){
$this->markers = split('%7C|\|',$_GET['markers']);
}
if($_GET['maptype']){
if(array_key_exists($_GET['maptype'],$this->tileSrcUrl)) $this->maptype = $_GET['maptype'];
}
}

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->offsetX = floor((floor($this->centerX)-$this->centerX)*$this->tileSize);
$this->offsetY = floor((floor($this->centerY)-$this->centerY)*$this->tileSize);
}

public function createBaseMap(){
$this->image = imagecreatetruecolor($this->width, $this->height);
$startX = floor($this->centerX-($this->width/$this->tileSize)/2);
$startY = floor($this->centerY-($this->height/$this->tileSize)/2);
$endX = ceil($this->centerX+($this->width/$this->tileSize)/2);
$endY = ceil($this->centerY+($this->height/$this->tileSize)/2);
$this->offsetX = -floor(($this->centerX-floor($this->centerX))*$this->tileSize);
$this->offsetY = -floor(($this->centerY-floor($this->centerY))*$this->tileSize);
$this->offsetX += floor($this->width/2);
$this->offsetY += floor($this->height/2);
$this->offsetX += floor($startX-floor($this->centerX))*$this->tileSize;
$this->offsetY += floor($startY-floor($this->centerY))*$this->tileSize;

for($x=$startX; $x<=$endX; $x++){
for($y=$startY; $y<=$endY; $y++){
$url = str_replace(array('{Z}','{X}','{Y}'),array($this->zoom, $x, $y), $this->tileSrcUrl[$this->maptype]);
$tileImage = imagecreatefromstring($this->fetchTile($url));
$destX = ($x-$startX)*$this->tileSize+$this->offsetX;
$destY = ($y-$startY)*$this->tileSize+$this->offsetY;
imagecopy($this->image, $tileImage, $destX, $destY, 0, 0, $this->tileSize, $this->tileSize);
}
}
}


public function placeMarkers(){
foreach($this->markers as $marker){
list($markerLat, $markerLon, $markerImage) = split(',',$marker);
$markerIndex++;
$markerFilename = $markerImage?(file_exists($this->markerBaseDir.'/'.$markerImage.".png")?$markerImage:'lightblue'.$markerIndex):'lightblue'.$markerIndex;
if(file_exists($this->markerBaseDir.'/'.$markerFilename.".png")){
$markerImg = imagecreatefrompng($this->markerBaseDir.'/'.$markerFilename.".png");
} else {
$markerImg = imagecreatefrompng($this->markerBaseDir.'/lightblue1.png');
}
$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)));
$destY = $destY - imagesy($markerImg);

imagecopy($this->image, $markerImg, $destX, $destY, 0, 0, imagesx($markerImg), imagesy($markerImg));
}
}



public function tileUrlToFilename($url){
return $this->tileCacheBaseDir."/".str_replace(array('http://'),'',$url);
}

public function checkTileCache($url){
$filename = $this->tileUrlToFilename($url);
if(file_exists($filename)){
return file_get_contents($filename);
}
}
public function mkdir_recursive($pathname, $mode){
is_dir(dirname($pathname)) || $this->mkdir_recursive(dirname($pathname), $mode);
return is_dir($pathname) || @mkdir($pathname, $mode);
}
public function writeTileToCache($url, $data){
$filename = $this->tileUrlToFilename($url);
$this->mkdir_recursive(dirname($filename),0777);
file_put_contents($filename, $data);
}
public function fetchTile($url){
if($this->useTileCache && ($cached = $this->checkTileCache($url))) return $cached;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0");
curl_setopt($ch, CURLOPT_URL, $url);
$tile = curl_exec($ch);
curl_close($ch);
if($this->useTileCache){
$this->writeTileToCache($url,$tile);
}
return $tile;

}

public function copyrightNotice(){
$logoImg = imagecreatefrompng($this->osmLogo);
imagecopy($this->image, $logoImg, imagesx($this->image)-imagesx($logoImg), imagesy($this->image)-imagesy($logoImg), 0, 0, imagesx($logoImg), imagesy($logoImg));
}

public function showMap(){
$this->parseParams();
$this->initCoords();
$this->createBaseMap();
if(count($this->markers))$this->placeMarkers();
if($this->osmLogo) $this->copyrightNotice();
header('Content-Type: image/png');
$expires = 60*60*24*14;
header("Pragma: public");
header("Cache-Control: maxage=".$expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
return imagepng($this->image);
}

}

$map = new staticMapLite();
print $map->showMap();

?>

Loading…
Cancel
Save