Matteusin

Mapa de Rotas - Mateus Ferreira

Aug 25th, 2015
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.74 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.       <title></title>
  5. </head>
  6. <body>
  7.     <script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.17&sensor=false"></script>
  8.     <script type="text/javascript">
  9.  
  10.  
  11. var directionsDisplay = [];
  12. var directionsService = [];
  13. var map = null;
  14.  
  15. function calcRoute() {
  16.   var msg = "41.077354,-81.511337:41.080647,-81.516579:41.077435,-81.521561:41.075253,-81.521492:41.074604,-81.520309:41.07415,-81.516335:41.073158,-81.514931:41.070534,-81.516563:41.066677,-81.516502:41.063942,-81.516502:41.06514,-81.513458:41.067383,-81.513412:41.069546,-81.513397:41.070778,-81.513382:41.072514,-81.512619:41.071106,-81.507614:41.073326,-81.506195";
  17.   var input_msg = msg.split(":");
  18.   var locations = new Array();
  19.  
  20.     var bounds = new google.maps.LatLngBounds();
  21.     for (var i = 0; i < input_msg.length; i++) {
  22.        var tmp_lat_lng = input_msg[i].split(",");
  23.        locations.push(new google.maps.LatLng(tmp_lat_lng[0], tmp_lat_lng[1]));
  24.        bounds.extend(locations[locations.length-1]);
  25.    }
  26.  
  27.    var mapOptions = {
  28.        // center: locations[0],
  29.        zoom: 12,
  30.        mapTypeId: google.maps.MapTypeId.ROADMAP
  31.    };
  32.    map = new google.maps.Map(document.getElementById('dvMap'), mapOptions);
  33.    map.fitBounds(bounds);
  34.    google.maps.event.addDomListener(window,'resize',function() {
  35.      google.maps.event.trigger(map,'resize');
  36.      map.fitBounds(bounds);
  37.    });
  38.  
  39.  var i = locations.length;
  40.  var index = 0;
  41.  
  42.  while (i != 0) {
  43.  
  44.    if (i < 3) {
  45.      var tmp_locations = new Array();
  46.      for (var j = index; j < locations.length; j++) {
  47.        tmp_locations.push(locations[index]);
  48.      }
  49.      drawRouteMap(tmp_locations);
  50.      i = 0;
  51.      index = locations.length;
  52.    }
  53.  
  54.    if (i >= 3 && i <= 10) {
  55.      console.log("before :fun < 10: i value " + i + " index value" + index);
  56.       var tmp_locations = new Array();
  57.       for (var j = index; j < locations.length; j++) {
  58.        tmp_locations.push(locations[j]);
  59.      }
  60.      drawRouteMap(tmp_locations);
  61.      i = 0;
  62.      index = locations.length;
  63.      console.log("after fun < 10: i value " + i + " index value" + index);
  64.    }
  65.  
  66.    if (i >= 10) {
  67.       console.log("before :fun > 10: i value " + i + " index value" + index);
  68.       var tmp_locations = new Array();
  69.       for (var j = index; j < index + 10; j++) {
  70.        tmp_locations.push(locations[j]);
  71.      }
  72.      drawRouteMap(tmp_locations);
  73.      i = i - 9;
  74.      index = index + 9;
  75.      console.log("after fun > 10: i value " + i + " index value" + index);
  76.     }
  77.   }
  78. }
  79.  
  80.  
  81. function drawRouteMap(locations) {
  82.  
  83.   var start, end;
  84.   var waypts = [];
  85.  
  86.   for (var k = 0; k < locations.length; k++) {
  87.    if (k >= 1 && k <= locations.length - 2) {
  88.      waypts.push({
  89.        location: locations[k],
  90.        stopover: true
  91.      });
  92.     }
  93.     if (k == 0) start = locations[k];
  94.  
  95.     if (k == locations.length - 1) end = locations[k];
  96.  
  97.   }
  98.   var request = {
  99.     origin: start,
  100.     destination: end,
  101.     waypoints: waypts,
  102.     travelMode: google.maps.TravelMode.DRIVING
  103.   };
  104.   console.log(request);
  105.  
  106.   directionsService.push(new google.maps.DirectionsService());
  107.   var instance = directionsService.length - 1;
  108.   directionsDisplay.push(new google.maps.DirectionsRenderer({
  109.     preserveViewport: true
  110.   }));
  111.   directionsDisplay[instance].setMap(map);
  112.   directionsService[instance].route(request, function(response, status) {
  113.     if (status == google.maps.DirectionsStatus.OK) {
  114.       console.log(status);
  115.       directionsDisplay[instance].setDirections(response);
  116.     }
  117.   });
  118. }
  119.  
  120. google.maps.event.addDomListener(window, 'load', calcRoute);
  121.     </script>
  122.     <div id="dvMap" style="width: 500px; height: 500px">
  123.     </div>
  124.   </body>
  125. </html>
Advertisement
Add Comment
Please, Sign In to add comment