Trying to create a routing algorithm over a bunch of arrays. Need some assistance

 var distanceStartOldY = null;
var distanceDestOldY = null;
 var distanceStartOldX = null;
var distanceDestOldX = null;
var distanceStartX = null;
var distanceStartY = null;
var distanceDestX = null;
var distanceDestY = null;
var distance=null;
var indexOfArrOld = null;
var start = [-0.870,50.492];
var destination = null;
var route = [];
var closestToStart = null;
var closestToDest = null;
var indexOfClosestToDest = null;
var indexOfClosestToStart = null;
var indexOfArr = 0;

function onUpdate {

if (generateRoute != null && Map != null) {
if (destination != null){
MapArr = WatchUi.loadResource(Rez.JsonData[Map[RoutePick]]);
	GenerateRoute();
}
        }
        
        }






function GenerateRoute(){

if (closestToDest == null || closestToDest[1] != destination[0] && closestToDest[0] != destination[1] || closestToStart[1] != destination[0] && closestToStart[0] != destination[1]){
for (var i=0;i<MapArr.size();i++){
var array = MapArr[i];
System.println("indexOfArr: " + indexOfArr);
for (var j=0;j<array.size()-2;j+=2){
var x1 = start[1];
var y1 = start[0];
var x2 = destination[1];
var y2 = destination[0];
var x3 = array[j];
var y3 = array[j+1];

  // Calculate the Euclidean distance between two points

  distanceStartX = x3 - x1;
  distanceStartY = y3 - y1;
  distanceDestX = x3 - x2;
  distanceDestY = y3 - y2; 

  System.println("sx " + distanceStartX.abs() + "sy " + distanceStartY.abs + "sx Old " + distanceStartOldX.abs() + "sy Old " + distanceStartOldY.abs());
  System.println("dx " + distanceDestX.abs() + "dy " + distanceDestY.abs + "dx Old " + distanceDestOldX.abs() + "dy Old " + distanceDestOldY.abs());
  
  if (distanceStartOldX != null){
  if (distanceStartX.abs() < distanceStartOldX.abs() && distanceStartY.abs() < distanceStartOldY.abs()){
   closestToStart = [array[j],array[j+1]];
    indexOfClosestToStart = j; 
    indexOfArr = i;
}
 if (distanceDestX.abs() < distanceDestOldX.abs() && distanceDestY.abs() < distanceDestOldY.abs()){
 closestToDest = [array[j],array[j+1]];
 indexOfClosestToDest = j; 
 }}
 distanceStartOldX = distanceStartX;
  distanceStartOldY = distanceStartY;
  distanceDestOldX = distanceDestX;
  distanceDestOldY = distanceDestY;}
 
 
 //System.println(closestToStart + "," + closestToDest);
 if (RoutePick == Map.size() - 1 && i == MapArr.size() - 1){
 var sarray = [closestToStart[1],closestToStart[0]];
 route.addAll(sarray);
 
 array = MapArr[indexOfArr];
 if (indexOfClosestToStart > indexOfClosestToDest){ 
 for (var k=indexOfClosestToStart;k>array[indexOfClosestToDest-2];k-=2){
 var pointLat=[array[k+1]];
 var pointLon=[array[k]];
  //System.println("startClosest: " + sarray);
 var points = [pointLat,pointLon];
 //System.println("points" + points);
 route.addAll(points);
 }
 }
  
 if (indexOfClosestToStart < indexOfClosestToDest){
 for (var l=indexOfClosestToStart;l<array[indexOfClosestToDest-2];l+=2){
 var pointLat=[array[l+1]];
 var pointLon=[array[l]];
  var points = [pointLat,pointLon];
  //System.println("points" + points);
 route.addAll(points);
 }
 }
 var destarr = [closestToDest[1],closestToDest[0]];
 route.addAll(destarr);
   //System.println("destClosest: " + destarr);
 RoutePick = 0;
 indexOfArrOld=indexOfArr;
 start = [closestToDest[0],closestToDest[1]];
}


if (RoutePick != Map.size()-1 && i == MapArr.size()-1){
RoutePick = RoutePick + 1;


WatchUi.requestUpdate();
}
}}
else if (closestToDest[1] == destination[0] && closestToDest[0] == destination[1] || closestToStart[1] == destination[0] && closestToStart[0] == destination[1]){
generateRoute = null;
WatchUi.requestUpdate;
}



}

So I have this code, it's a bit messy, my aim was to load jsonData, each one an array containing arrays of lines i.e jsonData = [[line1],[line2],[etc]] and load these arrays once per update to avoid triggering watchdog and affecting memory.

Then iterate through all the arrays of lines and find the closest point to start and the closest point to destination in the same line array as the closest point to start, then once i've iterated completely through all jsonData, save the current closestToStart and closestToDest into an array (including the points between them) set the next starting point to the current closestTo destination and reset the selected jsonData array to [0] again, and call onUpdate to repeat the function until a point closest to destination is found (if that makes sense). However the calculations don't appear to be working quite right.

It doesn't seem to select the closest point to start and destination but a random point in sweden (the jsonData represents the roads of northern europe, sweden is the last to be iterated through) which is nowhere near the random destination point around birmingham. I was hoping someone with a better eye for mistakes could guide me as to where I might've gone wrong ( oh and ignore the comment where it says calculate the euclidean distance, thats an old bit the forums wouldn't let me remove).

Thanks for any help.