Skip to content

Commit

Permalink
added BBox support to load only necessary markers
Browse files Browse the repository at this point in the history
  • Loading branch information
boombuler committed Feb 7, 2012
1 parent a871643 commit 1bc02d9
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 57 deletions.
42 changes: 22 additions & 20 deletions index-desktop.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,24 @@ function getGML(filter, display) {
display = "Unbearbeitet";

var filterurl = "./kml.php?filter="+filter;
var mygml = new OpenLayers.Layer.GML(display, filterurl, {
format: OpenLayers.Format.KML,

var mygml = new OpenLayers.Layer.Vector(display, {
projection: map.displayProjection,
formatOptions: {
extractStyles: true,
extractAttributes: true
}
strategies: [
new OpenLayers.Strategy.BBOX()
],
protocol: new OpenLayers.Protocol.HTTP({
url: filterurl,
format: new OpenLayers.Format.KML({
extractStyles: true,
extractAttributes: true
}),
})
});

map.addLayer(mygml);
return {
url: filterurl,
gml: mygml
}

return mygml;
}

//Initialise the 'map' object
Expand Down Expand Up @@ -212,21 +217,13 @@ function init() {
}
?>

var gmls = new Array();
for(var i = 0; i < gmlLayers.length; i++)
gmls.push(gmlLayers[i].gml);

selectControl = new OpenLayers.Control.SelectFeature(gmls,
selectControl = new OpenLayers.Control.SelectFeature(gmlLayers,
{onSelect: onFeatureSelect, onUnselect: onFeatureUnselect});

map.addControl(selectControl);
selectControl.activate();

var lonLat = new OpenLayers.LonLat(lon, lat).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());

var markerSize = new OpenLayers.Size(21,25);
var markerOffset = new OpenLayers.Pixel(-(markerSize.w/2), -markerSize.h);
icon = new OpenLayers.Icon('http://www.openstreetmap.org/openlayers/img/marker.png',markerSize,markerOffset);
map.setCenter (lonLat, zoom);
}

Expand Down Expand Up @@ -257,7 +254,12 @@ function change(id){
function gmlreload(){
for(var i = 0; i < gmlLayers.length; i++) {
var val = gmlLayers[i];
val.gml.setUrl(val.url);
//setting loaded to false unloads the layer//
val.loaded = false;
//setting visibility to true forces a reload of the layer//
val.setVisibility(true);
//the refresh will force it to get the new KML data//
val.refresh({ force: true, params: { 'random': Math.random()} });
}
}

Expand Down
93 changes: 56 additions & 37 deletions kml.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,45 @@

$filter = get_typ('filter');

echo '<?xml version="1.0" encoding="UTF-8"?>';?>
<kml xmlns="http://www.opengis.net/kml/2.2"><Document><name>PIRATEN</name><description><![CDATA[PIRATEN Wahlkampf Hilfe]]></description><?php
$dom = new DOMDocument('1.0', 'UTF-8');
$nodeKml = $dom->appendChild($dom->createElementNS('http://www.opengis.net/kml/2.2', 'kml'));

$nodeDoc = $nodeKml->appendChild($dom->createElement('Document'));
$nodeDoc->appendChild($dom->createElement('name', 'PIRATEN'));

$nodeDoc->appendChild($dom->createElement('description'))->appendChild($dom->createCDATASection('Piraten Plakate'));

// Define the styles
$styles = array();
$i = 0;
foreach($options as $key=>$value)
{
$styleKey = "s$i";
$i++;
$styles[$key] = $styleKey;
if (!($filter) || ($filter == $key)) {
?><Style id="<?php echo $styleKey?>"><IconStyle><hotSpot x="0.5" y="0.5" xunits="fraction" yunits="fraction" /><scale>0.6</scale><Icon><href>./images/markers/<?php echo $key?>.png</href></Icon></IconStyle></Style><?php
}
foreach($options as $key=>$value) {
$styleKey = "s$i";
$i++;
$styles[$key] = $styleKey;
if (!($filter) || ($filter == $key)) {
$nStyle = $nodeDoc->appendChild($dom->createElement('Style'));
$nStyle->setAttribute('id', $styleKey);
$nIconS = $nStyle->appendChild($dom->createElement('IconStyle'));
$nHotSpot = $nIconS->appendChild($dom->createElement('hotSpot'));
$nHotSpot->setAttribute('x', '0.5');
$nHotSpot->setAttribute('y', '0.5');
$nHotSpot->setAttribute('xunits', 'fraction');
$nHotSpot->setAttribute('yunits', 'fraction');
$nIconS->appendChild($dom->createElement('scale', '0.6'));
$nIconS->appendChild($dom->createElement('Icon'))->appendChild($dom->createElement('href', "./images/markers/$key.png"));
}
}
?>
<?php

$filterstr = "";
if ($filter) {
$filterstr = " AND type = '".mysql_escape($filter)."'";
}
$bbox = mysql_escape($_GET['bbox']);
if ($bbox) {
list($bbe, $bbn, $bbw, $bbs) = split(",", $bbox);
$filterstr .= " AND (f.lon >= $bbe) && (f.lon <= $bbw) && (f.lat >= $bbn) && (f.lat <= $bbs)";
}


$query = "SELECT p.id, f.lon, f.lat, f.type, f.user, f.timestamp, f.comment, f.image "
. " FROM ".$tbl_prefix."felder f JOIN ".$tbl_prefix."plakat p on p.actual_id = f.id"
Expand All @@ -72,47 +91,47 @@
$res = mysql_query($query) OR dieDB();
$num = mysql_num_rows($res);

for ($i=0;$i<$num;$i++)
{
for ($i=0;$i<$num;$i++) {
$id = mysql_result($res, $i, "id");

$lon = mysql_result($res, $i, "lon");
$arr = preg_split("/\./", $lon);
$ar2 = str_split($arr[1],6);
$lon = $arr[0].".".$ar2[0];

$lat = mysql_result($res, $i, "lat");
$arr = preg_split("/\./", $lat);
$ar2 = str_split($arr[1],6);
$lat = $arr[0].".".$ar2[0];

$type= mysql_result($res, $i, "type");

$user= mysql_result($res, $i, "user");

$time= mysql_result($res, $i, "timestamp");

$comment = mysql_result($res, $i, "comment");
if ($comment == null)
$comment = "";
$image = mysql_result($res, $i, "image");
if ($image == "")
$image = null;
?><Placemark><name><?php echo $id?></name><description><![CDATA[<?php
echo json_encode(array(
'id'=>$id,
't'=>$type,
'tb'=>$options[$type],
'i'=>htmlspecialchars($image),
'c'=>htmlspecialchars($comment),
'u'=>htmlspecialchars($user),
'd'=>date('d.m.y H:i', strtotime($time))
));
?>]]></description><?php
if (isset($options[$type]))
{
echo "<styleUrl>#".$styles[$type]."</styleUrl>";

$place = $nodeDoc->appendChild($dom->createElement('Placemark'));
$place->appendChild($dom->createElement('name', $id));
$place->appendChild($dom->createElement('description'))->appendChild(
$dom->createCDATASection(json_encode(array(
'id'=>$id,
't'=>$type,
'tb'=>$options[$type],
'i'=>htmlspecialchars($image),
'c'=>htmlspecialchars($comment),
'u'=>htmlspecialchars($user),
'd'=>date('d.m.y H:i', strtotime($time))
))));
if (isset($options[$type]))
$place->appendChild($dom->createElement('styleUrl', '#'.$styles[$type]));
$place->appendChild($dom->createElement('Point'))->appendChild($dom->createElement('coordinates', "$lon,$lat"));
}
?><Point><coordinates><?php echo $lon?>,<?php echo $lat?>,0.000000</coordinates></Point></Placemark><?php
}
?></Document></kml>
echo $dom->saveXML();
?>

0 comments on commit 1bc02d9

Please sign in to comment.