function ordenarArrayLatLng(array) {
    var novoArray = [];
    var indexes = [];

    //novoArray.push(array[0]);
    var i = 0;
    //indexes.push(i);
    do {
        var retorno = findClosestPolyline(array, i, indexes);
        i = retorno.Index;
        if (i >= 0) {
            novoArray.push(retorno.Array);
            indexes.push(i);
            console.log(i);
        }
    } while (novoArray.length < array.length && i != -1);

    return novoArray;
}

function findClosestPolyline(array, i, indexUsados) {
    var distancia = -1;
    var index = -1;
    var arrIn = array[i].getPath().getArray()[array[i].getPath().getArray().length - 1];
    for (var j in array) {
        if (j != i && indexUsados.indexOf(j) == -1) {
            var arrFim = array[j].getPath().getArray();
            dist = google.maps.geometry.spherical.computeDistanceBetween(arrIn, arrFim[0]);
            if (distancia == -1 || distancia > dist) {
                distancia = dist;
                index = j;
            }
            dist = google.maps.geometry.spherical.computeDistanceBetween(arrIn, arrFim[arrFim.length - 1]);
            if (distancia == -1 || distancia > dist) {
                distancia = dist;
                index = j;
            }
        }
    }

    return { Index: index, Array: array[index] };
}

function createEncodingsappendArray(array) {
    var novoArray = [];
    
    for (var i in array) {
        novoArray = novoArray.concat(array[i].getPath().getArray());
    }

    var objDistanciaGPS = new DistanciaGPS();

    novoArray = objDistanciaGPS.SimplificarPath(novoArray, 1000);

    return createEncodings(novoArray);
}


/*

"http://maps.googleapis.com/maps/api/staticmap?size=600x600&path=weight:5%7Ccolor:blue%7Cenc:" + createEncodings(arrayLatLng) + "&sensor=false";

*/

function createEncodings(coords) {
    var i = 0;

    var plat = 0;
    var plng = 0;

    var encoded_points = "";

    for (i = 0; i < coords.length; ++i) {
        
        if (coords[i]) {
        
            var lat = coords[i].lat();
            var lng = coords[i].lng();

            encoded_points += encodePoint(plat, plng, lat, lng);

            plat = lat;
            plng = lng;
        }
    }

    if (coords.length > 0) {
     
        if (coords && coords[0])
            encoded_points += encodePoint(plat, plng, coords[0][0], coords[0][1]);
    }

    return encoded_points;
}

function encodePoint(plat, plng, lat, lng) {
    var late5 = Math.round(lat * 1e5);
    var plate5 = Math.round(plat * 1e5)

    var lnge5 = Math.round(lng * 1e5);
    var plnge5 = Math.round(plng * 1e5)

    dlng = lnge5 - plnge5;
    dlat = late5 - plate5;

    return encodeSignedNumber(dlat) + encodeSignedNumber(dlng);
}

function encodeSignedNumber(num) {
    var sgn_num = num << 1;

    if (num < 0) {
        sgn_num = ~(sgn_num);
    }

    return (encodeNumber(sgn_num));
}

function encodeNumber(num) {
    var encodeString = "";

    while (num >= 0x20) {
        encodeString += (String.fromCharCode((0x20 | (num & 0x1f)) + 63));
        num >>= 5;
    }

    encodeString += (String.fromCharCode(num + 63));
    return encodeString;
}