Estoy escribiendo un programa de mapa interior de edificio de JavaScript University, y el código de ruta de visualización funciona, sin embargo, mi diccionario que almacena el gráfico se está cambiando dentro de la función y no tengo mucha experiencia en JavaScript y no puedo detectar el problema.
este es el Diccionario;
const TheGraph = { "A" : { "Coordinates" : [3776, 625], "Links" : {"C": 1} }, "B" : { "Coordinates" : [970, 910], "Links" : {"C" : 5, "G" : 5} }, .... }
Y aquí es donde creo que está el problema.
button.addEventListener('click', () =>{ var possRoutes = []; // stores the points const lineString = new H.geo.LineString(); var heading = (document.getElementById('room-header')).innerHTML var heading = heading.substring(5); // gets the name of the room for (const x of (Rooms[heading][1])){ possRoutes.push(findShortestPath(TheGraph, "A", x)) } // finds the shorted path possRoutes.reduce(function(prev, curr) { return prev.distance < curr.distance ? prev : curr; }); for (var cor in possRoutes[0].path){ var point = TheGraph[point = possRoutes[0].path[cor]].Coordinates // gets the coordinate from the dictionary var distanceToOrigin = pythagorean(point[1], point[0])//maths var ang = 1.5708-(angle + Math.atan(point[0]/point[1])) point[0] = originX-(Math.cos(ang)*distanceToOrigin)*scaleX point[1] = originY-(Math.sin(ang)*distanceToOrigin)*scaleY lineString.pushPoint({lat: point[0], lng: point[1]}) // adds the point to the map } point = Rooms[heading][0] var distanceToOrigin = pythagorean(point[1], point[0]) var ang = 1.5708-(angle + Math.atan(point[0]/point[1])) point[0] = originX-(Math.cos(ang)*distanceToOrigin)*scaleX point[1] = originY-(Math.sin(ang)*distanceToOrigin)*scaleY lineString.pushPoint({lat: point[0], lng: point[1]}) console.log(TheGraph) path = new H.map.Polyline( lineString, { style: { lineWidth: 4, strokeColor: '#2600ff' }} ) F0.addObject(path);//displays the route on the map })
const significa que el nombre theGraph
no se puede reasignar para referirse a otra cosa, no que la cosa a la que se refiere sea inmutable. Está cambiando los elementos en la matriz de coordenadas en las líneas que comienzan con point[0]
etc.
Esto solucionó mi problema.
var point = Object.assign({}, point);