Support two colors of points rendered on the map.

This commit is contained in:
George Kaklamanos 2016-02-20 22:02:18 +02:00
parent e3eb7b30f0
commit 2bb92abf4b
6 changed files with 56 additions and 26 deletions

View file

@ -42,7 +42,7 @@ function PrintRoutes($line){
} }
echo '</tbody></table>'; echo '</tbody></table>';
echo '</div><div id="map" class="tab-pane fade" role="tabpanel">'; echo '</div><div id="map" class="tab-pane fade" role="tabpanel">';
ShowMap(getPositionOfLine($line)); ShowMap(getPositionOfLine($line,'go'),getPositionOfLine($line,'come'));
echo '</div></div>'; echo '</div></div>';
} }
?> ?>

View file

@ -30,7 +30,7 @@ if (isValidRoute($_GET['route'])){
PrintStops($route,'go'); PrintStops($route,'go');
PrintStops($route,'come'); PrintStops($route,'come');
echo '</div><div id="map" class="tab-pane fade" role="tabpanel">'; echo '</div><div id="map" class="tab-pane fade" role="tabpanel">';
ShowMap(getPositionOfRoute($route['id'])); ShowMap(getPositionOfRoute($route['id'],'go'),getPositionOfRoute($route['id'],'come'));
echo '</div></div>'; echo '</div></div>';
} else { } else {
echo '<span style="display:inline-block" class="alert alert-danger" role="alert">No such route. <a href="javascript:history.back()">Go back?</a></span>'; echo '<span style="display:inline-block" class="alert alert-danger" role="alert">No such route. <a href="javascript:history.back()">Go back?</a></span>';

View file

@ -29,7 +29,7 @@ if (isValidStop($_GET["stop"])){
echo '<div id="routes" class="tab-pane fade in active" role="tabpanel">'; echo '<div id="routes" class="tab-pane fade in active" role="tabpanel">';
PrintRoutes($_GET["stop"]); PrintRoutes($_GET["stop"]);
echo '</div><div id="map" class="tab-pane fade" role="tabpanel">'; echo '</div><div id="map" class="tab-pane fade" role="tabpanel">';
ShowMap(array(array($stop['lat'],$stop['lon']))); ShowMap(array(array($stop['lat'],$stop['lon'])),[]);
echo '</div></div>'; echo '</div></div>';
} else { } else {
echo '<span style="display:inline-block" class="alert alert-danger" role="alert">No such stop. <a href="javascript:history.back()">Go back?</a></span>'; echo '<span style="display:inline-block" class="alert alert-danger" role="alert">No such stop. <a href="javascript:history.back()">Go back?</a></span>';

View file

@ -3,12 +3,14 @@
"id":"100", "id":"100",
"routeid":"10", "routeid":"10",
"lat":"35.3387", "lat":"35.3387",
"lon":"25.1317" "lon":"25.1317",
"direction":"go"
}, },
{ {
"id":"150", "id":"150",
"routeid":"20", "routeid":"20",
"lat":"35.4387", "lat":"35.4387",
"lon":"25.1217" "lon":"25.1217",
"direction":"come"
} }
] ]

View file

@ -112,21 +112,21 @@ function getLines(){
return $lines; return $lines;
} }
function getPositionOfRoute($rt){ function getPositionOfRoute($rt,$direction){
global $buspositions; global $buspositions;
$locations = array(); $locations = array();
foreach ($buspositions as $bus){ foreach ($buspositions as $bus){
if($bus["routeid"] == $rt){ if($bus["routeid"] == $rt && $bus["direction"] == $direction){
array_push($locations,array($bus["lat"],$bus["lon"])); array_push($locations,array($bus["lat"],$bus["lon"]));
} }
} }
return $locations; return $locations;
} }
function getPositionOfLine($line){ function getPositionOfLine($line,$direction){
$locations = array(); $locations = array();
foreach(getRoutesOfLine($line) as $route){ foreach(getRoutesOfLine($line) as $route){
foreach(getPositionOfRoute($route["id"]) as $pos){ foreach(getPositionOfRoute($route["id"],$direction) as $pos){
array_push($locations,$pos); array_push($locations,$pos);
} }
} }

View file

@ -2,36 +2,64 @@
<script src="openlayers/ol.js"></script> <script src="openlayers/ol.js"></script>
<?php <?php
function ShowMap($coords){ function ShowMap($coordsRed,$coordsGreen){
if(count($coords)>0){ if(count($coordsRed)>0 || count($coordsGreen)>0){
?> ?>
<div id="mymap" style="width:300px;height:300px;overflow:hidden;"></div> <div id="mymap" style="width:300px;height:300px;overflow:hidden;"></div>
<script> <script>
var style = new ol.style.Style({ var pos = new ol.proj.fromLonLat([25.1329,35.3342]);
var features = [];
<?php
if(count($coordsRed)>0){
echo "var coordinatesRed = [";
for ($i=0; $i<count($coordsRed)-1;$i++){
$loc = $coordsRed[$i];
echo '['. $loc[1] . ',' . $loc[0] . '],';
}
echo '['. $coordsRed[$i][1] . ',' . $coordsRed[$i][0] . ']];';
?>
var styleRed = new ol.style.Style({
image: new ol.style.Circle({ image: new ol.style.Circle({
fill: new ol.style.Fill({color: '#FF0000'}), fill: new ol.style.Fill({color: '#FF0000'}),
stroke: new ol.style.Stroke({color: '#000000'}), stroke: new ol.style.Stroke({color: '#000000'}),
radius: 5 radius: 5
}) })
}); });
<?php for(var i=0; i<coordinatesRed.length; i++){
echo "var coordinates = [";
for ($i=0; $i<count($coords)-1;$i++){
$loc = $coords[$i];
echo '['. $loc[1] . ',' . $loc[0] . '],';
}
echo '['. $coords[$i][1] . ',' . $coords[$i][0] . ']];';
?>
var pos = new ol.proj.fromLonLat([coordinates[0][0],coordinates[0][1]]);
var features = [];
for(var i=0; i<coordinates.length; i++){
var feat = new ol.Feature({ var feat = new ol.Feature({
geometry: new ol.geom.Point(new ol.proj.fromLonLat([coordinates[i][0],coordinates[i][1]])) geometry: new ol.geom.Point(new ol.proj.fromLonLat([coordinatesRed[i][0],coordinatesRed[i][1]]))
}); });
feat.setStyle(style); feat.setStyle(styleRed);
features.push(feat); features.push(feat);
} }
<?php
}
if(count($coordsGreen)>0){
echo "var coordinatesGreen = [";
for ($i=0; $i<count($coordsGreen)-1;$i++){
$loc = $coordsGreen[$i];
echo '['. $loc[1] . ',' . $loc[0] . '],';
}
echo '['. $coordsGreen[$i][1] . ',' . $coordsGreen[$i][0] . ']];';
?>
var styleGreen = new ol.style.Style({
image: new ol.style.Circle({
fill: new ol.style.Fill({color: '#00FF00'}),
stroke: new ol.style.Stroke({color: '#000000'}),
radius: 5
})
});
for(var i=0; i<coordinatesGreen.length; i++){
var feat = new ol.Feature({
geometry: new ol.geom.Point(new ol.proj.fromLonLat([coordinatesGreen[i][0],coordinatesGreen[i][1]]))
});
feat.setStyle(styleGreen);
features.push(feat);
}
<?php
}
?>
var vectorSource = new ol.source.Vector({features:features}); var vectorSource = new ol.source.Vector({features:features});
var vectorLayer = new ol.layer.Vector({ var vectorLayer = new ol.layer.Vector({
source: vectorSource, source: vectorSource,
@ -54,7 +82,7 @@ echo '['. $coords[$i][1] . ',' . $coords[$i][0] . ']];';
<?php <?php
}else{ }else{
?> ?>
<span style="display:inline-block" class="alert alert-danger" role="alert">No bus on its way yet. <a href="javascript:history.back()">Go back?</a></span> <span style="display:inline-block" class="alert alert-danger" role="alert">No information to display on the map yet (probably no bus is on its way). <a href="javascript:history.back()">Go back?</a></span>
<?php <?php
} }