Jump to content
Fórum Script Brasil
  • 0

Executar função com valores iguais JSON


Bruno More

Question

Galera, tenho uma dúvida que não deve ser complicada, provavelmente coisa de Noob lol.

Estou trabalhando num projeto com Google Maps. São registradas as rotas que os usuários fazem pelo celular (Latitudes e Longitudes) num BD MySQL. Preciso exibir as rotas no Google Maps que implementei no site. Estou enviando os dados da tabela para o JSON, onde o script do Google Maps executa as linhas de rota, porém, recria todas as rotas juntas, sem separação.

Meu problema: Reparem que no JSON abaixo, alguns campos possuem o valor route:1,route:2 e route:3 (O JSON abaixo possue 3 rotas apenas). Preciso separar todos os campos que possuem route:1, route:2, route:3 route:4, route:n. Abaixo segue o JSON e o JS que está executando.

[

{"id":"50","lat":"-23.629204","lng":"-46.530556","route":"1"},{"id":"51","lat":"-23.629204","lng":"-46.530556","route":"1"},

{"id":"52","lat":"-23.629204","lng":"-46.530556","route":"1"},{"id":"53","lat":"-23.629204","lng":"-46.530556","route":"1"},

{"id":"60","lat":"-23.630489","lng":"-46.528496","route":"1"},{"id":"61","lat":"-23.632305","lng":"-46.535473","route":"1"},

{"id":"62","lat":"-23.626974","lng":"-46.541965","route":"1"},{"id":"63","lat":"-23.629639","lng":"-46.538719","route":"1"},

{"id":"64","lat":"-23.569101","lng":"-46.647202","route":"2"},{"id":"65","lat":"-23.556120","lng":"-46.662617","route":"2"},

{"id":"66","lat":"-23.555929","lng":"-46.665573","route":"2"},{"id":"67","lat":"-23.558470","lng":"-46.668320","route":"2"},

{"id":"68","lat":"-23.560410","lng":"-46.671867","route":"2"},{"id":"69","lat":"-23.590410","lng":"-46.691868","route":"2"},

{"id":"70","lat":"-23.520411","lng":"-46.641869","route":"2"},{"id":"71","lat":"-23.523411","lng":"-46.698540","route":"3"},

{"id":"72","lat":"-23.634565","lng":"-46.599998","route":"3"},

]

var rota = new Array();

$.ajax({
    //...
    dataType: 'json',
    async: false,
    success: function(data) {
        $.each(data, function(index, value) {
                rota.push(new google.maps.LatLng(value.lat, value.lng));
        });
    }
});

//CONTRUTOR DE LINHAS DO GOOGLE MAPS
  var setRota1 = new google.maps.Polyline({
    path: rota,
    strokeColor: "#FFDD00",
    strokeOpacity: 0.4,
    strokeWeight: 4
  });

Espero que alguém possa me ajudar.

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

var vetor = [
{"id":"50","lat":"-23.629204","lng":"-46.530556","route":"1"},{"id":"51","lat":"-23.629204","lng":"-46.530556","route":"1"},
{"id":"52","lat":"-23.629204","lng":"-46.530556","route":"1"},{"id":"53","lat":"-23.629204","lng":"-46.530556","route":"1"},
{"id":"60","lat":"-23.630489","lng":"-46.528496","route":"1"},{"id":"61","lat":"-23.632305","lng":"-46.535473","route":"1"},
{"id":"62","lat":"-23.626974","lng":"-46.541965","route":"1"},{"id":"63","lat":"-23.629639","lng":"-46.538719","route":"1"},
{"id":"64","lat":"-23.569101","lng":"-46.647202","route":"2"},{"id":"65","lat":"-23.556120","lng":"-46.662617","route":"2"},
{"id":"66","lat":"-23.555929","lng":"-46.665573","route":"2"},{"id":"67","lat":"-23.558470","lng":"-46.668320","route":"2"},
{"id":"68","lat":"-23.560410","lng":"-46.671867","route":"2"},{"id":"69","lat":"-23.590410","lng":"-46.691868","route":"2"},
{"id":"70","lat":"-23.520411","lng":"-46.641869","route":"2"},{"id":"71","lat":"-23.523411","lng":"-46.698540","route":"3"},
{"id":"72","lat":"-23.634565","lng":"-46.599998","route":"3"}
];

function buscaRota(N,vetor) {
    var passosRota = [];
    for (i = 0; i < vetor.length; i++) {
        var passo = vetor[i];
        if (passo.route == N) passosRota.push(passo);
    }
    return passosRota;
}

var rota1 = buscaRota(3,vetor);
alert(rota1);

:)

Edited by fiote
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...