Problem using function with truncated file

Asked

Viewed 61 times

0

On another topic I did a question and a friend of the site helped me.

I tried to use the function with the truncated file, however the result was: inserir a descrição da imagem aqui

Code that I used:

<script>
function dec(string)
    {// retorna a matris pura, descompactada
        var myArray = [];
        var str, str2, arr, arr2, lat, lng, f;
        var strings = string.split(" ");
        for (var i in strings)
        {
            str = strings[i];
            str2 = '';
            arr = [];
            arr2 = [];
            for (var j=0; j<str.length; j++)
            {
                switch (str.charAt(j))
                {
                    case 'A': str2 += ',0'; break;
                    case 'B': str2 += ',1'; break;
                    case 'C': str2 += ',-1'; break;
                    case 'D': str2 += ',2'; break;
                    case 'E': str2 += ',-2'; break;
                    case 'F': str2 += ',3'; break;
                    case 'G': str2 += ',-3'; break;
                    case 'H': str2 += ',4'; break;
                    case 'I': str2 += ',-4'; break;
                    case 'J': str2 += ',5'; break;
                    case 'K': str2 += ',-5'; break;
                    case 'L': str2 += ',6'; break;
                    case 'M': str2 += ',-6'; break;
                    case 'N': str2 += ',7'; break;
                    case 'O': str2 += ',-7'; break;
                    case 'P': str2 += ',8'; break;
                    case 'Q': str2 += ',-8'; break;
                    case 'R': str2 += ',9'; break;
                    case 'S': str2 += ',-9'; break;
                    default: str2 += str.charAt(j); break;
                }
            }
            arr = str2.split(",");
            f = arr.shift();
            lng = parseInt(arr[0])/f;
            lat = parseInt(arr[1])/f;
            arr2.push([lat, lng]);
            for (var j=2; j<arr.length; j+=2)

            {
                lng += parseInt(arr[j])/f;
                lat += parseInt(arr[j+1])/f;
                arr2.push([lat, lng]);
            }
            myArray.push(arr2);
        }
        document.write(myArray);
        return myArray;
    }
dec('{shapes:{3550308:"1000I6575E3374BCBAACBABABABABBABBAABBBABBAABBABBDABAABBBABBAABBAABBABAABBBABBAABBBABBBBAABBAACBABCBABAABBABCBAACBCACBABAACBABAACBAAAACCAACCCACBCACACCACCCCCAACCCCCCACAACACACACACACACACACACCCACAACAACCCCCCCCCCACCACCCACCACCACACACBAACACACCAACACCAACACBAACACACCACCCACAACCACAACAACAACCACAACCACAACACBAACAAACACACCAACBCAAACACACACACACCAACACCAACACBAACACBAACBAACACACACACACCACAACACACACACCAACACCAACBAACACCAACCAACACACCCACACBAACBAACACACBAACACBAACBAACACBCBCBCACBAACACACBAACACACACACACACACACACACACACBCACACACACACCEACCEACACBAACACBAACBAACACAAACACBABCBAACBAABBBBABBBABABABABAABBABBBAABBBBBABBAABBBBBBBBABBBAABBBABBABAABBABABAABBAABBABBABBBBAABBBBAABBABABBABBABABBBABBBABAABBABABABBBABABABAABBABABABABABABABBBABABABCBABABAACBABBBABADABABABABABCBABABABCBABCBABCBABAACBABABAACBABABABABABABBACBABABBBABABABABAABBABABABABBBABABAABBAACBABABABAABBABAABCBABBABABAACBCBAABCAABBABAABABABBABAABBABABCBABABABAACBABAAAABBABBACBAACBABAABCAABBAABBABAABBABAACACBABAACCCCAACBABAACACBCBAABABBAACACBACCCABCBAACCACAACACCAACACBAACBABAABBAACBAACBAABABBAABABBABCBABAABBAABBAABBABABABAACBABABAABABBAABCAABABABBAABAAAABAACBAABAAABABBAABBAACACBAACACBAACACBAACBAACCAACBAACACACBAACBAACACBAACBCACBAACBAACACBAACBAACACACBAACACBAACBAACACBAACBAACACACACCAACACACACACCACACACBCACACAACCACCACBAACCAACCACACACACAACCACAACCAACACACCACACCAAACBABAACCAACCAACCACAACCAACCAACCAACCAACCBCACAACAAACBAACACACACACACBAACBCACBABAACBCACBAACACACCCCAECCACACAAEBCBAACACBACCACBAACAAACACBAACBABCBABAACBABABCBABABABAAAACACECCCACAAACCABCCCBAACBABAACACBABABAACACCACCCACACCCAACACBABCBAACACBAACACBCACBAACACBAACCAABCAABCAACCAACCAACCCACACACCAACACACBAACACCAACCACACACACACCCCCAABCACACACAACCCCCCAACAAACACBABCBAACBCACACACCAACCCCAACACACCAACCAACCAACCACCCACACACAACACCAACACBCACACACACCAACCACACCCCCACAACACACCAACACCACAACCACACACAACACACCACACAABABABABCACBABCAABCACACBCCCAACACACBAACCAACCACACACBCCCACACACACCCCCACACACACBCACACAABCACAABCACACACAACCACAABCACAABCAABCACACAABCAACCBACCACACACACACAABABCAABABCAABABCAABAAABAAABCAABABABABCAABCCABCACBCACACACAABCACAACCACACAACCAABCACCCACAABCAACCACAABCACACAACCACACCCACACAABCACBCAABCAABCAABABCAABCAABCACACACBABCACBCAABCACACACACACACACACACAACCBCACACACAABCACBCAABCACAABCACBCAABCAABCAABCACACACACAACCAACCAACCCACCACCCACACBCACBCBCACBCAABCACBCAABCAABCACAABCACBCBCACACAABCACAACACACACACACCAACACACAECCACAEAECEACACACCCAEACACBAACBAACACCCACACCACAACCAACACBCACAEACBACCACACCAACACCACCCAACCAACCACAACCACAACCACAACCAACCACACACAACCACAACCAACCACACACAACCACCCAACCACACAACCACAABCAABCAABCCEAACCCCAACCACBCAABCAACACCCACACACACACACACCAACACCACAACCAACACACACACBAACACCCCAACCAACCAACBAACACACCCACCCCCACBABABAACACBAACACACACBABABCACACBAACBAACBCBBBAACACACACBAABBCACBABABABAACBAACACBAACCAACCAACCAACCACAACCACACCCACACACCCCCCCCCCCCCCCCCAACCAACCACAACCAECCAECCAEACCCACCCABKBAAEBEACDSAEAEBEBCBEDELOBEFGDGJKACBCBCACACAGAC2ACACACACAKACACACACCCAEAECAACCCCCCAACCAACEECCCCCCACACACACACCEAGAGBEBOBEBCDCDADEDCDCDEDCACAEACACACACACACBCACACACACACACCAACACACACAECAACACACACACBCACBCCCBEBCCCBCACACACBAACACACACBAACHBEECCCAACACBAACACACACDAACBAACACCAACACBCACACACACCACAACBCBCACCACACAACACCAACCAAAEACAACACACBCACACBABABCDABBDAACBAACCCACCAACBCBECCBCCAACACCACAACCAACDEBAACBAACACACACCAACAAACCACAACBABACCBCACCCCACAACAACCACACACCAABABCACBCAACCACACCACCAACCACACACACAACCACACAACCCABCAACAACAACCAACCACAACCACAACBAACACACBCACBAACACACACACBAACBAACBABAACCAACCACACACCACCCCACBCAACCAACACCAACCACACAACCACACACCCACCCAAACAABCBABCBABCACAABCBABCACBCACAABABCBCBCACACACACACACCCACACACACACACACACBCACAABCAABCBABABCAABCACACCACCACCCACBCACACCACCAACCACCCAACCAACCACACCCACCCAABCACACACACCACCACCCACAACCAACCACACACACBCAABABCACACCCACACACACCCACACACACACAABCACBCAABCACACACACACACAACACCACACACACACAACBAACCACAAACACACACCCAACACCAACCCCCACCACACCACACCACACACAABCAACCAACCCCACACAABABCACAABABCAABBAABBABBBAABBABAABBAABABABBBABBBCBBAABBBABBAABBABAABABBAABBAABABABBAABBAABCAABABBABAABBAABBAABABABABCACAACCAABCAABCBCBCAABCAABCAABBAABABABCBCACBCACCCAABCACACAABCBCBABCAABABBAABCBABBBABCACACACACBCACAABCAABCACAABBAABCAABCAABCBCACBCACAACCAACCACACACBCBABCAABCACAABCACACACACACCCACBABABABABCBABABABABABCBABCBABABABABCBABABBACBCAABBAABABCAABABBAABCAABBACBBAABAAABABBAABBAABABBAABBAABABBBABBAABABABBAABCAABBACBABCAAACAABABABABAABABCBBACBABAACBAACACACACBAACBABAABBABABCBABADABABCBABAABBABADADABABBBABAABBJADABAFANAPAB0ABCAABCAABCBABCABBABCAABBAABABABABCAABABBABABABAACBABCBABBACBABBBABAACBAABBABABAABABBAABABABABABABABCACCABCAABBABAABBAABABABABABABCACBCAABCAABCAABCAABCAABCACAABCAABCACAABCACAABCACBCAABCACAABCACACACACAABCACAABCACACACACACACACACAABCACACAABCAABABABBAABAAABBAABCAABBAAABAABBAABBABAABABABABBAABBAABBBBAABABBABAABBAABCAAAABAABAAABBABCACAABBACBABAAABCBABABABBBABABBABBDBDABBBABABBABBBBBABBABBBABABAABBBBAABBDABBDABABABABCBCACBCACCCACAABCAABCAABCACAABCCACCAABCAABCAABCACAABCACBCBCAABCBBBABCACAABCACACBCAABABABABCBABCAABABCBABBAABDBBAABABBAABBBABBBCAABBAABABABCAABBAABABCAABABABABABAAABBAABABABBAABAAABCAABAAABCAABCACACACBCACACBCAABBABABAABBAABABCAABCBABABCBABCAABCAABBACBBAABABBAABABABABABBBABABBAABBAABABBAABABBAABABABABABABBAABABAAABABABABBAABABABABBAABABABABAAABCAABABBABBBBBAABABABBAABBBBABAABABBABABABAABBAABBABAABAAABBAABABABBAABBABAABBABAABBABAABBAABBAABABBAABBAABBAABBAABBBBAABBAABABABBAABBAABBABAABBAABBAACBAABBABAABABABABBBBAABBBCACAABABCACAABCACACBCACAABCAABABCBABCAACCAACCAACCAACCACACACCCACACACACACAACCCACCACAEBCCACCACACACACACAACCACAACCACACBCACCCACACAABCACACAACACCCCACACACCCACACAAAABCBABABAAABABCAABBAABCAABABABBAABABBBCAABBAABABBBABBAABABCAABCAABABABBABABABABBAABABABABAACBBBABAABABBABAABBAABABBABBBCBBBAABABBAABBABABCBAABBBBBABBABABABABBBABAABBAABABABABBABABDBAAABAACBABAABABABBAABABBABBABABBAABBAABABBAABABCAABABBAABCAABCBABABABBAABABABABABABABCAABBAABABABABABABDBBDBBBABABABABABABAABBAABABBDCACAACCACACAABCAABABABCACAABEACAABABAAABCACAABABCBAAABCACBABCAABABBAABBAABABBAABBAABBABABAABABABBAABAAABCAABCACAABCACACACACAABCACAABCAABABABCAABABCAABCBABABABABADABABABCAABCACAABCAABCAABABCBABABBAABABABBAABBABAABABBBABBABAABCACAACCACAACCAACACCCCCACCACACACACAABCAACCAACCACACACAABCAABCAABCACAABCAAACACAACCACAACCCACBAACCAACACACCACAACCACBBBABCAABCBCBCBCBABCAABABABABABABABCAABCAABABCAABABABABABABABCAABBBABCAABCACAABABABABABCAABCAABCAABCAABABABCACACBCAABCAABCAABCBABABABABBAABBABAABABABABABCAABCAABABCACAABCAABCBABBAABBAABCAABCBBAABBAABBAABBAACBAABBAABBABAAABAABBCBABABAABBABABCBABAABBAABAABAABBABABAABCAABBABAACBAACBAACBAACACCAACCAACBAACBAABBAACBABACBABBAABBABABABAACBAACAAAABABAACAAACBABAACBABBBABAABABABCAABABBABAABBABABCBABABACBBBBABABABABAAAABABAAABBAABCAABBAABABABABBAACBAABABBABAABBAABBAACBABCBAABBAACBAABBABCBABABAACACBAACBABABAABBAAABAACBAABBCABBABAAABABAACBBBABABABABBBAACBAACACBAACBAACBAACBABABAACBABABABABAACBAACBABCACACACACACACBAACBCBCBABABABABABBACBAACACBCACACACACACACBAAABAACBABABABABCABBABAACACACACACACACBABABABABABABBBABCBCBCBCBABAABBBABABABABABABBABBBAABBABABAABBABBACBADEBCBABABABABAACBAACBCBCACBABCBABABCBCACBCACBAACBABABBBABABBBABABABABBBABABABABADBBABCBABAACBABABBBAABABBABABDBBBABADABAABBAABABCBCBABBABABAACBABADABABABAABBACBCBABABABABBABAABBACDABABBBBBABBBABBAABBAABBBBAABBABABBBABBBAABABABCBCAABABABBABBBABAABBABBBABABABBBBBBABBABABAABBAABBAABBAABBABAABABBBBABABABABA"},bounds:{NORTE:-23.3563,SUL:-24.0084,LESTE:-46.3651,OESTE:-46.8262}}');

</script>

Does anyone know that I was wrong to use the function?

  • What part is giving NaN,NaN...?

  • 1

    Something else, don’t use for (var i in strings), for strings is an array. Use for(var i=0; i<strings.length; i++).

  • Nan, Nan, myArray leaves..., he gets like this after he passes the switch. I used for(var i in strings) because the function is already ready,

  • And which is the entrance (string) that you’re using?

  • @bfavaretto This link here:http://www.cites.ibge.gov.br/gmap/shapes/35/MU_M13_3550308_1000.json

1 answer

2

What you are doing wrong is passing your entire JSON to the function. It expects to receive only key content shapes. View code in operation here.

I don’t know how you get this JSON, depending on the case you will need to decode it as an object before you can access this key.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.