Advertisement
Ostap34PHP

Untitled

Aug 17th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         var geocoder = new google.maps.Geocoder();
  2.         geocoder.geocode({'address': 'Ternopil'}, function (results, status) {
  3.             if (status == google.maps.GeocoderStatus.OK) {
  4.                 var start_lat = results[0].geometry.location.lat();
  5.                 var start_lng = results[0].geometry.location.lng();
  6.                 alert(start_lat);
  7.                 alert(start_lng);
  8.                 geocoder.geocode({'address': 'Lviv'}, function (results, status) {
  9.                     if (status == google.maps.GeocoderStatus.OK) {
  10.                         var end_lat = results[0].geometry.location.lat();
  11.                         var end_lng = results[0].geometry.location.lng();
  12.                         alert(end_lat);
  13.                         alert(end_lng);
  14.  
  15.                         //start budlocode
  16.                         var marker = false;
  17.                         var fullTime = 86640;
  18.                         var pathTime = 18353;
  19.  
  20.                         var map = new google.maps.Map(document.getElementById("map"), {
  21.                             zoom: 4,
  22.                             center: new google.maps.LatLng(start_lat, start_lng),
  23.                             mapTypeId: google.maps.MapTypeId.ROADMAP
  24.                         });
  25.                         var directionsDisplay = new google.maps.DirectionsRenderer();
  26.                         var directionsService = new google.maps.DirectionsService();
  27.                         directionsDisplay.setOptions({suppressMarkers: true, suppressInfoWindows: true});
  28.                         directionsDisplay.setMap(map);
  29.                         var request = {
  30.                             origin: new google.maps.LatLng(start_lat, start_lng),
  31.                             destination: new google.maps.LatLng(end_lat, end_lng),
  32.                             travelMode: google.maps.DirectionsTravelMode.DRIVING
  33.                         };
  34.                         directionsService.route(request, function (response, status) {
  35.                             if (status == google.maps.DirectionsStatus.OK) {
  36.                                 directionsDisplay.setDirections(response);
  37.                                 updateMarker(response.routes[0].legs[0], 0);
  38.                                 setInterval(function () {
  39.                                     updateMarker(response.routes[0].legs[0], 5);
  40.                                 }, 5000);
  41.                                 console.log(response);
  42.                             }
  43.                         });
  44.  
  45.                         function updateMarker(legs, delta) {
  46.                             if (marker) marker.setMap(null);
  47.                             var markerIcon = '{{ asset('img/auto.png') }}';
  48.                             if (pathTime + delta > 0) pathTime = pathTime + delta;
  49.                             else {
  50.                                 marker = new google.maps.Marker({position: legs.steps[0].lat_lngs[0], map: map});
  51.                                 marker.setIcon(markerIcon);
  52.                                 return;
  53.                             }
  54.                             if (pathTime > fullTime) pathTime = fullTime;
  55.                             var points = getDirectionPoints(legs);
  56.                             var countPoints = 0;
  57.                             for (i = 0; i < points.length; i++) countPoints = countPoints + points[i];
  58.                             var curentPoint = parseInt(pathTime * countPoints / fullTime);
  59.                             var step = getStep(points, curentPoint);
  60.                             marker = new google.maps.Marker({
  61.                                 position: legs.steps[step[0]].lat_lngs[step[1] - 1],
  62.                                 map: map
  63.                             });
  64.                             marker.setIcon(markerIcon);
  65.                         }
  66.  
  67.                         function getStep(points, curentPoint) {
  68.                             for (var i = 0; i < points.length; i++) {
  69.                                 curentPoint = curentPoint - points[i];
  70.                                 if (curentPoint <= 0) return [i, curentPoint + points[i]];
  71.                             }
  72.                         }
  73.  
  74.                         function getDirectionPoints(data) {
  75.                             var points = [];
  76.                             for (var i = 0; i < data.steps.length; i++) {
  77.                                 if (data.steps[i].lat_lngs) {
  78.                                     points.push(data.steps[i].lat_lngs.length);
  79.                                 }
  80.                             }
  81.                             return points;
  82.                         }
  83.  
  84.                         //endbudlocode
  85.                     }
  86.                 });
  87.             }
  88.         });
  89.         //////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement