Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var geocoder = new google.maps.Geocoder();
- geocoder.geocode({'address': 'Ternopil'}, function (results, status) {
- if (status == google.maps.GeocoderStatus.OK) {
- var start_lat = results[0].geometry.location.lat();
- var start_lng = results[0].geometry.location.lng();
- alert(start_lat);
- alert(start_lng);
- geocoder.geocode({'address': 'Lviv'}, function (results, status) {
- if (status == google.maps.GeocoderStatus.OK) {
- var end_lat = results[0].geometry.location.lat();
- var end_lng = results[0].geometry.location.lng();
- alert(end_lat);
- alert(end_lng);
- //start budlocode
- var marker = false;
- var fullTime = 86640;
- var pathTime = 18353;
- var map = new google.maps.Map(document.getElementById("map"), {
- zoom: 4,
- center: new google.maps.LatLng(start_lat, start_lng),
- mapTypeId: google.maps.MapTypeId.ROADMAP
- });
- var directionsDisplay = new google.maps.DirectionsRenderer();
- var directionsService = new google.maps.DirectionsService();
- directionsDisplay.setOptions({suppressMarkers: true, suppressInfoWindows: true});
- directionsDisplay.setMap(map);
- var request = {
- origin: new google.maps.LatLng(start_lat, start_lng),
- destination: new google.maps.LatLng(end_lat, end_lng),
- travelMode: google.maps.DirectionsTravelMode.DRIVING
- };
- directionsService.route(request, function (response, status) {
- if (status == google.maps.DirectionsStatus.OK) {
- directionsDisplay.setDirections(response);
- updateMarker(response.routes[0].legs[0], 0);
- setInterval(function () {
- updateMarker(response.routes[0].legs[0], 5);
- }, 5000);
- console.log(response);
- }
- });
- function updateMarker(legs, delta) {
- if (marker) marker.setMap(null);
- var markerIcon = '{{ asset('img/auto.png') }}';
- if (pathTime + delta > 0) pathTime = pathTime + delta;
- else {
- marker = new google.maps.Marker({position: legs.steps[0].lat_lngs[0], map: map});
- marker.setIcon(markerIcon);
- return;
- }
- if (pathTime > fullTime) pathTime = fullTime;
- var points = getDirectionPoints(legs);
- var countPoints = 0;
- for (i = 0; i < points.length; i++) countPoints = countPoints + points[i];
- var curentPoint = parseInt(pathTime * countPoints / fullTime);
- var step = getStep(points, curentPoint);
- marker = new google.maps.Marker({
- position: legs.steps[step[0]].lat_lngs[step[1] - 1],
- map: map
- });
- marker.setIcon(markerIcon);
- }
- function getStep(points, curentPoint) {
- for (var i = 0; i < points.length; i++) {
- curentPoint = curentPoint - points[i];
- if (curentPoint <= 0) return [i, curentPoint + points[i]];
- }
- }
- function getDirectionPoints(data) {
- var points = [];
- for (var i = 0; i < data.steps.length; i++) {
- if (data.steps[i].lat_lngs) {
- points.push(data.steps[i].lat_lngs.length);
- }
- }
- return points;
- }
- //endbudlocode
- }
- });
- }
- });
- //////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement