Error: Array Out Of Bounds Error Details: Failed invoking <symbol> Help! No idea whats wrong.

The error is at line line 11, I don't really understand what's wrong with the array or how it's out of bounds, this code was shared with me so I'm not 100% sure how it works as I'm very much a coding noob.

I'm trying to draw a route.

var track = Application.Storage.getValue("track");
        var lon = Application.Storage.getValue("tracklon");
        var lat = Application.Storage.getValue("tracklat");

 if(track != null && lon != null && lat != null){
        var lonMax = 180.0;
        var lonMin = -180.0;
        var latMax = 90.0;
        var latMin = -90.0;
            for(var i=0; i < track.size(); i++) {
            lonMax = (lonMax > lon[i]) ? lonMax : lon[i];
            lonMin = (lonMin < lon[i]) ? lonMax : lon[i];
            latMax = (latMax > lat[i]) ? latMax : lat[i];
            latMin = (latMin < lat[i]) ? latMax : lat[i];
            }
            var scaleX = dc.getWidth() / (lonMax - lonMin);
            var scaleY = dc.getHeight() / (latMax - latMin);
            var scaleXY = (scaleX < scaleY) ? scaleX : scaleY;
            for(var i=0; i<track.size();i++){
                var pixelsLon = (lon[i+1] - lon[i]);
                var pixelsLat = (lat[i+1] - lat[i]);
                
                var displayX = pixelsLon + 120;
                var displayY = pixelsLat + 120;
               
                dc.setColor(Graphics.COLOR_WHITE, Graphics.COLOR_TRANSPARENT);
                dc.fillCircle(displayX, displayY, 3);
                dc.setColor(Graphics.COLOR_BLACK, Graphics.COLOR_TRANSPARENT);
                dc.fillCircle(displayX, displayY, 2);
        }}

  • You got it.

    Sorry, it's another typo from me due to copy&paste.

    So it should read like this:

    lonMax = (lonMax > lon[i]) ? lonMax : lon[i];
    lonMin = (lonMin < lon[i]) ? lonMin : lon[i];
    latMax = (latMax > lat[i]) ? latMax : lat[i];
    latMin = (latMin < lat[i]) ? latMin : lat[i];

    However, I think that you should not just copy&paste the code. I wanted to give you an understanding of how to solve the problem, but my "pseudo code" must be adapted to your needs.

    Also, you need to use double precision because the increments of lat/lon are usually in the range of 1.0e-6, i.e. in the 6th digit.

  • In my project I have used some of what I’ve learnt in the past from implementing other features and applied it to other things so I’m sure I will find other ways to apply what I’ve learnt here for other things in this or future projects or even tweak it here and there. I just need to see how something works before I can grasp it.

    Thanks again for sharing your coding wizardry.