Untitled
unknown
plain_text
4 years ago
4.0 kB
6
Indexable
const {ipcRenderer} = require('electron') let locationCoordinates = []; var locationCount="" ipcRenderer.invoke('get-latlongs').then(()=> console.log('')).catch((err)=> console.error('Error')); ipcRenderer.on("send-latlongs", (e, args) => { const receivedLists= JSON.parse(args); locationCoordinates= receivedLists; console.log(locationCoordinates) }); let directionsService; let directionsDisplay; function initMap() { locationCount=locationCoordinates.length; /*var distanceMatrix = [] for (var i = 0; i < locationCount; i++) { distanceMatrix[i] = new Array(locationCount); } for (var i = 0; i < locationCount; i++) { for (var j = 0; j < locationCount; j++) { distanceMatrix[i][j] = calcDistance(locationCoordinates[i], locationCoordinates[j]); } } */ //const start = new google.maps.LatLng(40.765470, 29.940592); //kargo başlangıç yeri const start = { lat: 40.765470, lng: 29.940592 }; var options = { zoom: 15, center: { lat: 40.765470, lng: 29.940592 }, } const map = new google.maps.Map(document.getElementById("map"), options); directionsService = new google.maps.DirectionsService(); directionsDisplay = new google.maps.DirectionsRenderer(); directionsDisplay.setMap(map) var shortestpath = shortestPath(locationCoordinates, locationCount, start); showRoute(start, shortestpath[1]); for (var i = 0; i < locationCount - 1; i++) { showRoute(shortestpath[i], shortestpath[i + 1]) } } function shortestPath(locationCoordinates, locationCount, start) { var pathLength = 99999999999999 var temp var tempArr var arr var fact = factorial(locationCount); var p = permute(locationCoordinates); for (var i = 0; i < fact; i++) { tempArr = p.next().value temp = calcPathLength(tempArr, locationCount, start) if (temp < pathLength) { pathLength = temp arr = tempArr } } return arr } function factorial(locationCount) { if (locationCount == 0) { return 1; } return locationCount * factorial(locationCount - 1); } function permute(arr) { var l = arr.length, used = Array(l), data = Array(l); return function* backtracking(pos) { if (pos == l) yield data.slice(); else for (var i = 0; i < l; ++i) if (!used[i]) { used[i] = true; data[pos] = arr[i]; yield* backtracking(pos + 1); used[i] = false; } }(0); } function showRoute(start, destination) { let request = { origin: start, destination: destination, travelMode: google.maps.TravelMode.DRIVING }; directionsService.route(request, function (response, status) { if (status == 'OK') { directionsDisplay.setDirections(response); let starter = new google.maps.Marker({ position: start, map: map }) let marker = new google.maps.Marker({ position: destination, map: map }) } }) } function calcDistance(start, destination) { let request = { origin: start, destination: destination, travelMode: google.maps.TravelMode.DRIVING }; directionsService.route(request, function (response, status) { if (status == 'OK') { var distance = response.routes[0].legs[0].distance.value; console.log(distance) } else { console.log("Distance couldn't be calculated.") } return distance }) } function calcPathLength(array, length, start) { var sum = calcDistance(start, array[0]); for (var i = 0; i < length - 1; i++) { sum = sum + calcDistance(array[i], array[i + 1]); } return sum; }
Editor is loading...