Прошу оказать помощь с JavaScript
Добрый день:victory:
Есть необходимость в google api. Задача следующая: google maps наносится массив маркеров. Далее при нажатии на маркер должен построиться маршрут с точки А (точка А(старт) для всех маркеров(финиш) будет константа). В infoWindow к маркеру вылазит километраж от точки А(старт) к маркеру(финиш, на который кликнули). Google api при большом количестве запросов платный - а определяться нужная точка будет визуально, поэтому построение маршрута и расчёт киллометража пытаюсь произвести в момент нажатия на маркер. Наваял следующий код, но имею проблеммы с работоспособностью, прошу помощи белых богов и гуру. Буду признательно благодарен за оказанную помощь! <!DOCTYPE html> <html> <head> <title>Outlets</title> <meta name="viewport" content="initial-scale=1.0"> <meta charset="utf-8"> <style> html, body { height: 100%; margin: 0; padding: 0; } #map { height: 100%; } </style> </head> <body> <div id="map"></div> <script> var centrMap = {lat: 50.4375, lng: 30.4615}; var outlet = []; var infoOutl = []; var outlName = []; var outlLat = []; var outlLng = []; var totalKM = []; outlName[0] = 'Fst'; outlName[1] = 'Sec'; outlName[2] = 'Th'; outlLat[0] = 50.5606; outlLat[1] = 50.7606; outlLat[2] = 50.9606; outlLng[0] = 30.6232; outlLng[1] = 30.8232; outlLng[2] = 31.0232; var imageWH = 'http://google-maps-icons.googlecode.com/files/factory.png'; var imageOutl = 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png'; function initMap() { var directionsDisplay = new google.maps.DirectionsRenderer; var directionsService = new google.maps.DirectionsService; var map = new google.maps.Map(document.getElementById('map'), { zoom: 13, center: centrMap }); var wh = new google.maps.Marker({ position: centrMap, title: 'Warehouse!', icon: imageWH }); wh.setMap(map); var infoWH = new google.maps.InfoWindow({ content: 'WAREHOUSE!' }); wh.addListener('click', function() { infoWH.open(map, wh); }); // MARKERS BLOCK for(var j = 0; j < outlName.length; j++){ outlet[j] = new google.maps.Marker({ position: {lat: outlLat[j], lng: outlLng[j]}, title: outlName[j], map: map, icon: imageOutl }); outlet[j].addListener('click', function() { calculateAndDisplayRoute(directionsService, directionsDisplay, j); computeTotalDistance(directionsDisplay.directions, j); infoOutl[j] = new google.maps.InfoWindow({ content: totalKM[j] }); infoOutl[j].open(map, outlet[j]); directionsDisplay.setMap(map); }); } } function calculateAndDisplayRoute(directionsService, directionsDisplay, k) { directionsService.route({ origin: centrMap, destination: {lat: outlLat[k], lng: outlLng[k]}, travelMode: google.maps.TravelMode.DRIVING, unitSystem: google.maps.UnitSystem.METRIC }, function(response, status) { if (status == google.maps.DirectionsStatus.OK) { directionsDisplay.setDirections(response); } else { window.alert('Directions request failed due to ' + status); } }); } function computeTotalDistance(result, o) { var total = 0; var myroute = result.routes[0]; for (i = 0; i < myroute.legs.length; i++) { total += myroute.legs[i].distance.value; } total = total / 1000. totalKM[o] = total + " км"; } </script> <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&signed_in=true&callback=initMa p" async defer></script> </body> </html> |
Часовой пояс GMT +3, время: 16:43. |