Untitled

 avatar
unknown
javascript
4 years ago
6.7 kB
12
Indexable
const { ipcRenderer } = require('electron')
let locationCoordinates = [];
var locationCount = ""

//let directionsService;
//let directionsDisplay;
function initMap() {


    /*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


    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)

    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)
        locationCount = locationCoordinates.length;
        const start = { lat: 40.765470, lng: 29.940592 };
        var end = { lat: 40.76124439803489, lng: 29.9436904870682 };
        //  var start = '40.765470, 29.940592';
        //  var end = '40.76124439803489, 29.9436904870682';
        /**
         * 
         * @param {*} start 
         * @param {*} destination 
         * @returns void
         */
        function showRoute(start, destination, array, length) {
            let waypts = []
            console.log(length)
            for (var i = 0; i < length - 1; i++) {
                waypts.push({
                    location: array[i],
                    stopover: true
                });

            }
            let request = {
                origin: start,
                destination: end,
                waypoints: waypts,
                optimizeWaypoints: true,
                travelMode: google.maps.TravelMode.DRIVING
            };
            directionsService.route(request, function (response, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    var distance = 0
                        //directionsDisplay.setDirections(response);
                        for (var i = 0; i < length - 1; i++) {
                            distance = distance + response.routes[0].legs[i].distance.value
            
                        }
                        console.log("DISTANCE", distance);
                    directionsDisplay.setDirections(response);
                    let starter = new google.maps.Marker({
                        position: start,
                        map: map
                    })
                    let marker = new google.maps.Marker({
                        position: destination,
                        map: map
                    })
                }
            })
        }

        //  var end={lat:'40.76124439803489' ,lng:' 29.9436904870682'}
        function factorial(locationCount) {

            if (locationCount == 0) {
                return 1;

            }
            return locationCount * factorial(locationCount - 1);
        }

        async function shortestPath(locationCoordinates, locationCount, start) {
            var pathLength = 99999999999999
            var temp
            var tempArr
            var arr
            var fact = factorial(locationCount);
            var p = permute(locationCoordinates);
            try {
                for (var i = 0; i < fact; i++) {
                    tempArr = p.next().value
                    temp = await calcPathLength(tempArr, locationCount, start)
                    if (temp < pathLength) {
                        pathLength = temp
                        arr = tempArr
                    }
                    console.log(temp)
                }
            } catch (e) {
                console.log("shortestPath", e);
            }
            console.log(arr)
            return arr
        }
        shortestPath(locationCoordinates, locationCount, start).then(shortestpath => {
            showRoute(start, shortestpath[length-1],shortestpath,locationCount);
        })



        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 calcPathLength(array, length, start) {
            let destination = array[length - 1]
            let waypts = []
            console.log(length)
            for (var i = 0; i < length - 1; i++) {
                waypts.push({
                    location: array[i],
                    stopover: true
                });

            }
            return new Promise((resolve, reject) => {
                let request = {
                    origin: start,
                    destination: destination,
                    waypoints: waypts,
                    optimizeWaypoints: true,
                    travelMode: google.maps.TravelMode.DRIVING
                };
                directionsService.route(request, function (response, status) {
                    console.log("DIRACTION", status);
                    if (status == 'OK') {
                        var distance = 0
                        //directionsDisplay.setDirections(response);
                        for (var i = 0; i < length - 1; i++) {
                            distance = distance + response.routes[0].legs[i].distance.value
            
                        }
                        console.log("DISTANCE", distance);
                        resolve(distance)
                        //localStorage.setItem("Dist", distance);
                    } else {
                        console.log("calcDistance REJECT", response);
                        reject(response) // error
                    }
                })
            })



            //localStorage.removeItem("Dist");


        }
    });

}
Editor is loading...