The Map (google maps API - javascript) stops working when I add a function in the script!

Asked

Viewed 98 times

1

The map stops when I put the Connect function, when I remove it the map will appear again as always

  var map;
  var markers = [];
  function initMap() {
    map = new google.maps.Map(document.getElementById('map'), {
      center: {lat: 75.15975385, lng: -75.15975385},
      zoom: 8
    });
    LigaTudo();
  }
     var i array_nome, array_lat, array_lng ,string_arraynm, string_arraylt, string_arraylg;
string_arraynm= “<?php echo $string_arraynm; ?>“;
 string_arraylt= “<?php echo $string_arraylt; ?>“;
   string_arraylg= “<?php echo $string_arraylg ?>“;
     array_nome = string_arraynm.split(“|”);
       array_lat = string_arraylt.split(“|”);
         array_lng = string_arraylg.split(“|”);
         function LigaTudo(){
          for (var i = array_nome.length - 1; i >= 0; i--) {
            var local = new google.maps.LatLng(array_lat[i],array_lng[i]);

            var mark = new google.maps.Marker({
position: local,
title: $array_nome[i]
});

          }
         }
</script>
<script type="text/javascript" src="pontinhos.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=minhachave&callback=initMap"
async defer></script>

The above is where the functions are, below I leave php(I don’t know if it can be something in it).

<?php
$pontonm = array();
$pontolt = array();
$pontolg = array();
// Aqui você se conecta ao banco
$mysqli = new mysqli('localhost', 'root', 'root', 'tcc');
// Executa uma consulta que pega cinco notícias
$sql = "SELECT `nome_pub`,`lat_pub`,`lng_pub` FROM `publicos`";
$query = $mysqli->query($sql);
while ($dados = $query->fetch_assoc()) {


 array_push($pontonm,$dados['nome_pub']);
 array_push($pontolt,$dados['lat_pub']);
 array_push($pontolg,$dados['lng_pub']);
   }




$string_arraynm = implode ( "|",$pontonm);
$string_arraylt = implode ( "|",$pontolt);
$string_arraylg = implode ( "|",$pontolg);
 ?>

I wonder if someone could identify the problem, and how do I use the function (she shoves the database data to create markers) without the map stops working, thanks in advance.

  • checked if there is an error in the script? because your function LigaTudo() has a for controlled by the size of array_nome and within traverses the variables array_lat[i] e array_lng[i]; case the size of array_nome is bigger than any of these other two, it sucks..

No answers

Browser other questions tagged

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