Advertisement
Bedhoel

Direction Map

Sep 21st, 2018
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.12 KB | None | 0 0
  1. <?php
  2. $origin_lat       = $_REQUEST['origin_lat'];
  3. $origin_long      = $_REQUEST['origin_long'];
  4.  
  5. $destination_lat  = $_REQUEST['destination_lat'];
  6. $destination_long = $_REQUEST['destination_long'];
  7. $zoom = (empty($_REQUEST['zoom']) ? '17' : $_REQUEST['zoom']);  // 1 to 22
  8. ?>
  9. <!DOCTYPE html>
  10. <html>
  11.   <head>
  12.     <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
  13.     <meta charset="utf-8">
  14.     <title>Direction</title>
  15.     <style>
  16.       /* Always set the map height explicitly to define the size of the div
  17.        * element that contains the map. */
  18.       #map {
  19.         height: 100%;
  20.       }
  21.       /* Optional: Makes the sample page fill the window. */
  22.       html, body {
  23.         height: 100%;
  24.         margin: 0;
  25.         padding: 0;
  26.       }
  27.       #floating-panel {
  28.         position: absolute;
  29.         top: 10px;
  30.         left: 25%;
  31.         z-index: 5;
  32.         background-color: #fff;
  33.         padding: 5px;
  34.         border: 1px solid #999;
  35.         text-align: center;
  36.         font-family: 'Roboto','sans-serif';
  37.         line-height: 30px;
  38.         padding-left: 10px;
  39.       }
  40.     </style>
  41.   </head>
  42.   <body>
  43.     <div id="floating-panel">
  44.         <b>Mode of Travel: </b>
  45.         <select id="mode">
  46.           <option value="DRIVING">Driving</option>
  47.           <option value="WALKING">Walking</option>
  48.           <option value="BICYCLING">Bicycling</option>
  49.           <option value="TRANSIT">Transit</option>
  50.         </select>
  51.     </div>
  52.     <div id="map"></div>
  53.     <script>
  54.         function initMap() {
  55.         var directionsDisplay = new google.maps.DirectionsRenderer;
  56.         var directionsService = new google.maps.DirectionsService;
  57.         var map = new google.maps.Map(document.getElementById('map'), {
  58.           zoom: <?php echo $zoom ?>,
  59.           center: {lat: <?php echo $origin_lat ?>, lng: <?php echo $origin_long ?>}
  60.         });
  61.         directionsDisplay.setMap(map);
  62.  
  63.         calculateAndDisplayRoute(directionsService, directionsDisplay);
  64.         document.getElementById('mode').addEventListener('change', function() {
  65.           calculateAndDisplayRoute(directionsService, directionsDisplay);
  66.         });
  67.       }
  68.  
  69.       function calculateAndDisplayRoute(directionsService, directionsDisplay) {
  70.         var selectedMode = document.getElementById('mode').value;
  71.         directionsService.route({
  72.           origin: {lat: <?php echo $origin_lat ?>, lng: <?php echo $origin_long ?>},  
  73.           destination: {lat: <?php echo $destination_lat ?>, lng: <?php echo $destination_long ?>},  
  74.           // Note that Javascript allows us to access the constant
  75.           // using square brackets and a string value as its
  76.           // "property."
  77.           travelMode: google.maps.TravelMode[selectedMode]
  78.         }, function(response, status) {
  79.           if (status == 'OK') {
  80.             directionsDisplay.setDirections(response);
  81.           } else {
  82.             window.alert('Directions request failed due to ' + status);
  83.           }
  84.         });
  85.       }
  86.     </script>
  87.     <script async defer
  88.     src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap&libraries=geometry,places">
  89.     </script>
  90.   </body>
  91. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement