From: maxious Date: Sun, 21 Nov 2010 08:44:43 +0000 Subject: Better trip planning form, autozooming for static map display based on inserted markers X-Git-Url: https://maxious.lambdacomplex.org/git/?p=bus.git&a=commitdiff&h=829a9a552f7d9eb2f81cd51ffca6b7fb8c4067ef --- Better trip planning form, autozooming for static map display based on inserted markers --- --- a/busui/common.inc.php +++ b/busui/common.inc.php @@ -19,10 +19,15 @@ bus.lambdacomplex.org - '.$pageTitle.' - + + - @@ -34,7 +39,6 @@

'.$pageTitle.'

- Home
'; } @@ -87,31 +91,71 @@ return $f; } -function staticmap($mapPoints) +function staticmap($mapPoints, $zoom = 0, $markerImage = "iconb") { $width = 300; $height = 300; -$zoom = 0; +$metersperpixel[9]=305.492*$width; +$metersperpixel[10]=152.746*$width; +$metersperpixel[11]=76.373*$width; +$metersperpixel[12]=38.187*$width; +$metersperpixel[13]=19.093*$width; +$metersperpixel[14]=9.547*$width; +$metersperpixel[15]=4.773*$width; +$metersperpixel[16]=2.387*$width; +// $metersperpixel[17]=1.193*$width; $center = ""; $markers = ""; - - if (sizeof($mapPoints) < 1) return ""; +$minlat = 999; +$minlon = 999; +$maxlat = 0; +$maxlon = 0; + + if (sizeof($mapPoints) < 1) return "map error"; if (sizeof($mapPoints) === 1) { - $zoom = 14; - $markers .= $mapPoint[0].",".$mapPoint[1].",ol-marker"; + if ($zoom == 0) $zoom = 14; + $markers .= "{$mapPoints[0][0]},{$mapPoints[0][1]},$markerimage"; $center = "{$mapPoints[0][0]},{$mapPoints[0][1]}"; } else { foreach ($mapPoints as $index => $mapPoint) { - $markers .= $mapPoint[0].",".$mapPoint[1].",lightblue".($index+1); + $markers .= $mapPoint[0].",".$mapPoint[1].",".$markerImage.($index+1); if ($index+1 != sizeof($mapPoints)) $markers .= "|"; + if ($mapPoint[0] < $minlat) $minlat = $mapPoint[0]; + if ($mapPoint[0] > $maxlat) $maxlat = $mapPoint[0]; + if ($mapPoint[1] < $minlon) $minlon = $mapPoint[1]; + if ($mapPoint[1] > $maxlon) $maxlon = $mapPoint[1]; $totalLat += $mapPoint[0]; $totalLon += $mapPoint[1]; } - $zoom = 11; + if ($zoom == 0) { + $mapwidthinmeters = distance($minlat,$minlon,$minlat,$maxlon); + foreach (array_reverse($metersperpixel,true) as $zoomLevel => $maxdistance) + { + if ($zoom == 0 && $mapwidthinmeters < ($maxdistance + 50)) $zoom = $zoomLevel; + } + } $center = $totalLat/sizeof($mapPoints).",".$totalLon/sizeof($mapPoints); } return ''; +} + +function distance($lat1, $lng1, $lat2, $lng2) +{ + $pi80 = M_PI / 180; + $lat1 *= $pi80; + $lng1 *= $pi80; + $lat2 *= $pi80; + $lng2 *= $pi80; + + $r = 6372.797; // mean radius of Earth in km + $dlat = $lat2 - $lat1; + $dlng = $lng2 - $lng1; + $a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlng / 2) * sin($dlng / 2); + $c = 2 * atan2(sqrt($a), sqrt(1 - $a)); + $km = $r * $c; + + return $km * 1000; } function decodePolylineToArray($encoded) @@ -202,7 +246,8 @@ $url = "http://geocoding.cloudmade.com/daa03470bb8740298d4b10e3f03d63e6/geocoding/v2/find.js?query=".$query."&bbox=-35.5,149.00,-35.15,149.1930&return_location=true&bbox_only=true"; $contents = json_decode(getPage($url)); if ($giveOptions) return $contents->features; - else return $contents->features[0]->centroid->coordinates[0].",".$contents->features[0]->centroid->coordinates[1]; + elseif (isset($contents->features[0]->centroid)) return $contents->features[0]->centroid->coordinates[0].",".$contents->features[0]->centroid->coordinates[1]; + else return ""; } function reverseGeocode($lat,$lng) { @@ -210,6 +255,16 @@ $contents = json_decode(getPage($url)); return $contents->features[0]->properties->name; } + +function startsWith($haystack,$needle,$case=true) { + if($case){return (strcmp(substr($haystack, 0, strlen($needle)),$needle)===0);} + return (strcasecmp(substr($haystack, 0, strlen($needle)),$needle)===0); +} + +function endsWith($haystack,$needle,$case=true) { + if($case){return (strcmp(substr($haystack, strlen($haystack) - strlen($needle)),$needle)===0);} + return (strcasecmp(substr($haystack, strlen($haystack) - strlen($needle)),$needle)===0); +} ?> --- /dev/null +++ b/busui/staticmaplite/.gitignore @@ -1,1 +1,3 @@ +cache/tiles +cache/map --- /dev/null +++ b/busui/staticmaplite/images/markers/GPlotter - Make Google Maps Easily.URL @@ -1,1 +1,3 @@ +[InternetShortcut] +URL=http://gplotter.offwhite.net/ --- /dev/null +++ b/busui/staticmaplite/images/markers/Google Maps Icons, Free!.URL @@ -1,1 +1,3 @@ +[InternetShortcut] +URL=http://brennan.offwhite.net/blog/2005/07/23/new-google-maps-icons-free/ --- a/busui/staticmaplite/index.html +++ b/busui/staticmaplite/index.html --- /dev/null +++ b/busui/staticmaplite/selinux-fix.sh @@ -1,1 +1,3 @@ +chcon -R -t httpd_sys_content_rw_t cache + --- a/busui/staticmaplite/staticmap.php +++ b/busui/staticmaplite/staticmap.php @@ -41,10 +41,10 @@ protected $osmLogo = 'images/osm_logo.png'; protected $useTileCache = true; - protected $tileCacheBaseDir = 'cache/tiles'; + protected $tileCacheBaseDir = './cache/tiles'; protected $useMapCache = true; - protected $mapCacheBaseDir = 'cache/maps'; + protected $mapCacheBaseDir = './cache/maps'; protected $mapCacheID = ''; protected $mapCacheFile = ''; protected $mapCacheExtension = 'png'; @@ -190,8 +190,7 @@ public function mkdir_recursive($pathname, $mode){ - is_dir(dirname($pathname)) || $this->mkdir_recursive(dirname($pathname), $mode); - return is_dir($pathname) || @mkdir($pathname, $mode); + return mkdir($pathname, $mode, true); } public function writeTileToCache($url, $data){ $filename = $this->tileUrlToFilename($url); @@ -271,3 +270,4 @@ print $map->showMap(); ?> + --- a/busui/stopList.php +++ b/busui/stopList.php @@ -1,6 +1,14 @@ + +
+ '; echo ' '; -echo' -
-
- - '; + include_footer(); ?> --- a/busui/tripPlanner.php +++ b/busui/tripPlanner.php @@ -1,48 +1,115 @@ From: {$tripplan->plan->from->name} "; - echo "

To: {$tripplan->plan->to->name}

"; - echo "

At: {$tripplan->plan->date}

"; - foreach ($tripplan->plan->itineraries->itinerary as $itineraryNumber => $itinerary ){ - echo "

Option #".($itineraryNumber+1)."

"; - echo "Duration: ".floor($itinerary->duration/60000)." minutes ({$itinerary->startTime} to {$itinerary->endTime})
\n"; - echo "Walking time: ".floor($itinerary->walkTime/60000)." minutes ({$itinerary->walkDistance} meters)
\n"; - echo "Transit time: ".floor($itinerary->transitTime/60000)." minutes
\n"; - echo "Waiting time: ".floor($itinerary->waitingTime/60000)." minutes
\n"; - $legMarkers = Array(); - foreach ($itinerary->legs->leg as $legNumber => $leg ){ - $legMarkers[] = Array($leg->from->lat,$leg->from->lon); - } - echo staticmap($legMarkers)."
\n"; - foreach ($itinerary->legs->leg as $legNumber => $leg ){ - echo "Leg #".($legNumber+1)." (from: {$leg->from->name} to {$leg->to->name}, ".floor($leg->duration/60000)." minutes)
\n"; - $legArray = object2array($leg); - if ($legArray["@mode"] === "BUS") { - echo "Take bus {$legArray['@route']} ".str_replace("To","towards",$legArray['@headsign'])."
"; - } - else { - foreach ($leg->steps->walkSteps as $stepNumber => $step ){ - echo "Walking step $stepNumber $step->absoluteDirection / $step->relativeDirection on $step->streetName for $step->distance meters / ".floor($step->duration/60000)." minutes
\n"; - } - } - } - } -} else { - echo '
I would like to go from to - on at -
'; + include('common.inc.php'); + include_header("Trip Planner"); + function tripPlanForm($errorMessage = "") + { + $from = (isset($_REQUEST['from']) ? $_REQUEST['from'] : "Brigalow"); + $to = (isset($_REQUEST['to']) ? $_REQUEST['to'] : "Barry"); + $date = (isset($_REQUEST['date']) ? $_REQUEST['date'] : date("m/d/Y")); + $time = (isset($_REQUEST['time']) ? $_REQUEST['time'] : date("h:ia")); + echo "$errorMessage"; + echo '
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
'; + } + + function processItinerary($itineraryNumber, $itinerary) + { + echo '
0 ? 'data-collapsed="true"' : "") . '>

Option #' . ($itineraryNumber + 1) . ": " . floor($itinerary->duration / 60000) . " minutes ({$itinerary->startTime} to {$itinerary->endTime})

"; + echo "Walking time: " . floor($itinerary->walkTime / 60000) . " minutes (" . floor($itinerary->walkDistance) . " meters)
\n"; + echo "Transit time: " . floor($itinerary->transitTime / 60000) . " minutes
\n"; + echo "Waiting time: " . floor($itinerary->waitingTime / 60000) . " minutes
\n"; + -} - include_footer(); + + if (is_array($itinerary->legs->leg)) { + $legMarkers = array(); + foreach ($itinerary->legs->leg as $legNumber => $leg) { + $legMarkers[] = array($leg->from->lat, $leg->from->lon); + } + echo '' . staticmap($legMarkers) . "
\n"; + echo '

    '; + foreach ($itinerary->legs->leg as $legNumber => $leg) { + echo '
  • '; + processLeg($legNumber, $leg); + echo "
  • "; + } + echo "
"; + } else { + echo '' . staticmap(array(array($itinerary->legs->leg->from->lat, $itinerary->legs->leg->from->lon))) . "
\n"; + processLeg(0, $itinerary->legs->leg); + } + + echo "

"; + } + + function processLeg($legNumber, $leg) { + $legArray = object2array($leg); + echo '

Leg #' . ($legNumber + 1) . " ( {$legArray['@mode']} from: {$leg->from->name} to {$leg->to->name}, " . floor($leg->duration / 60000) . " minutes)

\n"; + if ($legArray["@mode"] === "BUS") { + echo "Take bus {$legArray['@route']} " . str_replace("To", "towards", $legArray['@headsign']) . "
"; + } else { + $walkStepMarkers = array(); + foreach ($leg->steps->walkSteps as $stepNumber => $step) { + $walkStepMarkers[] = array($step->lat, $step->lon); + } + echo "" . staticmap($walkStepMarkers, "icong") . "
\n"; + foreach ($leg->steps->walkSteps as $stepNumber => $step) { + echo "Walking step " . ($stepNumber + 1) . " $step->absoluteDirection / $step->relativeDirection on $step->streetName for " . floor($step->distance) . " meters
\n"; + } + } + } + + if ($_REQUEST['time']) { + $toPlace = (startsWith($_REQUEST['to'], "-") ? $_REQUEST['to'] : geocode(urlencode($_REQUEST['to']), false)); + $fromPlace = (startsWith($_REQUEST['from'], "-") ? $_REQUEST['from'] : geocode(urlencode($_REQUEST['from']), false)); + if ($toPlace == "" || $fromPlace == "") { + $errorMessage = ""; + if ($toPlace === "") + $errorMessage .= urlencode($_REQUEST['to']) . " not found.
\n"; + if ($fromPlace === "") + $errorMessage .= urlencode($_REQUEST['from']) . " not found.
\n"; + tripPlanForm($errorMessage); + } else { + $url = "http://localhost:8080/opentripplanner-api-webapp/ws/plan?_dc=1290254798856&arriveBy=false&date=" . urlencode($_REQUEST['date']) . "&time=" . urlencode($_REQUEST['time']) . "&mode=TRANSIT%2CWALK&optimize=QUICK&maxWalkDistance=840&wheelchair=false&toPlace=$toPlace&fromPlace=$fromPlace&intermediatePlaces="; + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_HEADER, 0); + curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json")); + $page = curl_exec($ch); + curl_close($ch); + $tripplan = json_decode($page); + echo "

From: {$tripplan->plan->from->name} To: {$tripplan->plan->to->name}

"; + echo "

At: {$tripplan->plan->date}

"; + + if (is_array($tripplan->plan->itineraries->itinerary)) { + echo '
'; + foreach ($tripplan->plan->itineraries->itinerary as $itineraryNumber => $itinerary) { + processItinerary($itineraryNumber, $itinerary); + } + echo "
"; + } else { + processItinerary(0, $tripplan->plan->itineraries->itinerary); + } + + } + } else { + tripPlanForm(); + } + include_footer(); ?>