Advertisement
Ostap34PHP

Untitled

Aug 17th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         var marker = false;
  2.         var fullTime = {{ $duration['value'] }};
  3.         var pathTime = 0;
  4.  
  5.         var map = new google.maps.Map(document.getElementById("map"), {
  6.             zoom: 4,
  7.             //center: new google.maps.LatLng(39.31252574424125, -100.65206356448482),
  8.             mapTypeId: google.maps.MapTypeId.ROADMAP
  9.         });
  10.         var directionsDisplay = new google.maps.DirectionsRenderer();
  11.         var directionsService = new google.maps.DirectionsService();
  12.         directionsDisplay.setOptions({suppressMarkers: true, suppressInfoWindows: true});
  13.         directionsDisplay.setMap(map);
  14.         var request = {
  15.             origin: new google.maps.LatLng({{ $start['lat'] }}, {{ $start['lng'] }}),
  16.             destination: new google.maps.LatLng({{ $end['lat'] }}, {{ $end['lng'] }}),
  17.             travelMode: google.maps.DirectionsTravelMode.DRIVING
  18.         };
  19.         directionsService.route(request, function (response, status) {
  20.             if (status == google.maps.DirectionsStatus.OK) {
  21.                 directionsDisplay.setDirections(response);
  22.                 updateMarker(response.routes[0].legs[0], 0);
  23.                 /*setInterval(function () {
  24.                     updateMarker(response.routes[0].legs[0], 5);
  25.                 }, 5000);*/
  26.                 console.log(response);
  27.             }
  28.         });
  29.  
  30.         function updateMarker(legs, delta) {
  31.             if (marker) marker.setMap(null);
  32.             var markerIcon = '{{ asset('img/auto.png') }}';
  33.             if (pathTime + delta > 0) pathTime = pathTime + delta;
  34.             else {
  35.                 marker = new google.maps.Marker({position: legs.steps[0].lat_lngs[0], map: map});
  36.                 marker.setIcon(markerIcon);
  37.                 return;
  38.             }
  39.             if (pathTime > fullTime) pathTime = fullTime;
  40.             var points = getDirectionPoints(legs);
  41.             var countPoints = 0;
  42.             for (i = 0; i < points.length; i++) countPoints = countPoints + points[i];
  43.             var curentPoint = parseInt(pathTime * countPoints / fullTime);
  44.             var step = getStep(points, curentPoint);
  45.             marker = new google.maps.Marker({position: legs.steps[step[0]].lat_lngs[step[1] - 1], map: map});
  46.             marker.setIcon(markerIcon);
  47.         }
  48.  
  49.         function getStep(points, curentPoint) {
  50.             for (var i = 0; i < points.length; i++) {
  51.                 curentPoint = curentPoint - points[i];
  52.                 if (curentPoint <= 0) return [i, curentPoint + points[i]];
  53.             }
  54.         }
  55.  
  56.         function getDirectionPoints(data) {
  57.             var points = [];
  58.             for (var i = 0; i < data.steps.length; i++) {
  59.                 if (data.steps[i].lat_lngs) {
  60.                     points.push(data.steps[i].lat_lngs.length);
  61.                 }
  62.             }
  63.             return points;
  64.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement