2016-08-05 5 views
2
<script type='text/javascript'> 
    jQuery(document).ready(function($){ 
     var geocoder; 
     var map; 
     var markersArray = []; 
     var infos = []; 

     geocoder = new google.maps.Geocoder(); 
     var myOptions = { 
       zoom: 9, 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      } 
     var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
     map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
     var bounds = new google.maps.LatLngBounds(); 
     var encodedString; 
     var stringArray = []; 
     encodedString = document.getElementById("encodedString").value; 
     stringArray = encodedString.split("****"); 

     var x; 
     for (x = 0; x < stringArray.length; x = x + 1) 
     { 
      var addressDetails = []; 
      var marker; 
      addressDetails = stringArray[x].split("&&&"); 
      var lat = new google.maps.LatLng(addressDetails[1], addressDetails[2]); 
      marker = new google.maps.Marker({ 
       map: map, 
       position: lat, 
       //Content is what will show up in the info window 
       content: addressDetails[0] 
      }); 

      markersArray.push(marker); 
      google.maps.event.addListener(marker, 'click', function() { 
       closeInfos(); 
       var info = new google.maps.InfoWindow({content: this.content}); 
       //On click the map will load the info window 
       info.open(map,this); 
       infos[0]=info; 
      }); 
      //Extends the boundaries of the map to include this new location 
      bounds.extend(lat); 
     } 
     //Takes all the lat, longs in the bounds variable and autosizes the map 
     map.fitBounds(bounds); 

     //Manages the info windows 
     function closeInfos(){ 
     if(infos.length > 0){ 
      infos[0].set("marker",null); 
      infos[0].close(); 
      infos.length = 0; 
     } 
     } 

}); 
</script> 

</head> 
<body> 
<div id='input'> 

    <?php 
    $encodedString = ""; 
    $x = 0; 

    $result = mysql_query("SELECT * FROM `table-name`"); 
    while ($row = mysql_fetch_array($result, MYSQL_NUM)) 
    { 
     if ($x == 0) 
     { 
      $separator = ""; 
     } 
     else 
     { 
      $separator = "****"; 
     } 
     $encodedString = $encodedString.$separator. 
     "<p class='content'><b>Lat:</b> ".$row[1]. 
     "<br><b>Long:</b> ".$row[2]. 
     "<br><b>Name: </b>".$row[3]. 
     "<br><b>Address: </b>".$row[4]. 
     "</p>&&&".$row[1]."&&&".$row[2]; 
     $x = $x + 1; 
    }   
    ?> 

    <input type="hidden" id="encodedString" name="encodedString" value="<?php echo $encodedString; ?>" /> 
</div> 
<div id="map_canvas"></div> 

Ich habe oben Code für Google Map Standort von MySQL-Datenbank verwendet.Zu viel recurssion in google map

Über Code fetch Lat, Lang aus MySQL-Datenbank und dynamisch erstellen Google Map.

Wenn ich über Code ausführen Es gab mir Fehler wie folgt aus:

Es gibt zwei too much recursion & initMap is not function genannt Fehler.

Kann jemand mir helfen, es zu sortieren.

Vielen Dank im Voraus.

Antwort

1

Sie haben initMap nicht in Ihrer js-Datei definiert. Deshalb bekommst du diesen Fehler. Sie müssen wahrscheinlich den Rückruf Teil von der google map api Skript

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap" async defer></script> 

Um

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY" async defer></script> 

auch prüfen, ob die lat und lon enthalten gültige Zahlenwerte in der folgenden Aussage

var lat = new google.maps.LatLng(addressDetails[1], addressDetails[2]); 
entfernen