Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP/Javascript-在while循环中从mysql数据库在google地图上显示标记_Javascript_Php_Google Maps_Google Maps Api 3 - Fatal编程技术网

PHP/Javascript-在while循环中从mysql数据库在google地图上显示标记

PHP/Javascript-在while循环中从mysql数据库在google地图上显示标记,javascript,php,google-maps,google-maps-api-3,Javascript,Php,Google Maps,Google Maps Api 3,我有一个while循环,它显示我数据库中的数据,如姓名地址等。我希望谷歌地图能够循环显示数据库中的地址。目前,谷歌地图只显示一次,我不认为它的循环与地图旁边列出的地址相同 <script src="https://maps.google.com/maps/api/js?key=[KEY-HERE]&signed_in=true&callback=initMap"async defer></script> while ($row = mysql_fetch

我有一个while循环,它显示我数据库中的数据,如姓名地址等。我希望谷歌地图能够循环显示数据库中的地址。目前,谷歌地图只显示一次,我不认为它的循环与地图旁边列出的地址相同

<script src="https://maps.google.com/maps/api/js?key=[KEY-HERE]&signed_in=true&callback=initMap"async defer></script>

while ($row = mysql_fetch_assoc($query)) {

echo "<div class='result-container'>

    <div class='result-wrap'>

        <div class='leftbox'> 

            <div class='titlebox'>
                <h2>" . $row['company_name'] . "</h2>
            </div>

            <div class='box'>
                " . ((empty($row['address_1'])) ? '' : "<strong> Address: </strong>" . $row['address_1']) . ((empty($row['address_2'])) ? '' : ",&nbsp" . $row['address_2']) . ((empty($row['town'])) ? '' : ",&nbsp" . $row['town']) . ((empty($row['county'])) ? '' : ",&nbsp" . $row['county']) . ((empty($row['postcode'])) ? '' : ",&nbsp" . $row['postcode']) . "
            </div>

            <div class='box'>
                " . ((empty($row['tel'])) ? '' : "<strong> Telephone: </strong>" . $row['tel']) . ((empty($row['mobile'])) ? '' : "<strong> Mobile: </strong>" . $row['mobile']) . "
            </div>          

        </div> 

        <div class='rightbox'>

                <div class='service-pic'>
                 <img src='img/buddy.png' class='hvr-bob'>
                </div>                      

            <div id='map' style='width: 250px; height: 250px;'></div> 

        </div>

        </div><br/>";

  }

<script> 
    var address = "$row['address_1'], $row['town'], $row['postcode'], UK";  

        var map = new google.maps.Map(document.getElementById('map'), { 
            mapTypeId: google.maps.MapTypeId.TERRAIN,
            zoom: 6
        });

        var geocoder = new google.maps.Geocoder();

        geocoder.geocode({
            'address': address
            }, 
        function(results, status) {
            if(status == google.maps.GeocoderStatus.OK) {
                new google.maps.Marker({
                position: results[0].geometry.location,
                map: map
                });
                map.setCenter(results[0].geometry.location);
            }
        });
</script>

while($row=mysql\u fetch\u assoc($query)){
回声“
“$row[‘公司名称’]”
“((空($row['address\u 1']))?”(地址:“$row['address\u 1'])。((空($row['address\u 2']))?”,$row['address\u 2'])。((空($row['town']),$row['town.)。((空($row['county']))?”,“,$row['county'])。((空($row['county']))),“,$row['county'])。((空($row['postcode.))),“)。”
“((空($row['tel'])?”:“电话:”。$row['tel'])。((空($row['mobile'])?”:“移动:”。$row['mobile'])”

“; } var address=“$row['address_1'],$row['town'],$row['postcode'],英国”; var map=new google.maps.map(document.getElementById('map'),{ mapTypeId:google.maps.mapTypeId.TERRAIN, 缩放:6 }); var geocoder=new google.maps.geocoder(); 地理编码({ “地址”:地址 }, 功能(结果、状态){ if(status==google.maps.GeocoderStatus.OK){ 新的google.maps.Marker({ 位置:结果[0]。geometry.location, 地图:地图 }); map.setCenter(结果[0].geometry.location); } });
如果我正确理解了问题,您需要在页面上创建地图的多个实例

必须为脚本标记中指定为回调的
initMap()
JavaScript函数生成代码。此函数将创建映射的多个实例,为此,我们可以引入
$counter
变量,以确保我们确实有不同的实例。此外,我还从脚本标记中删除了一个signed_-in参数,因为它最近被弃用了

代码片段如下所示:

 <script src="https://maps.google.com/maps/api/js?key=[KEY-HERE]&callback=initMap" async defer></script>

    <?php
         $counter = 0;
         $jsInitFunction = "function initMap() { var geocoder = new google.maps.Geocoder(); ";
         while ($row = mysql_fetch_assoc($query)) {
            echo "<div class='result-container'>

              <div class='result-wrap'>

                  <div class='leftbox'>

                      <div class='titlebox'>
                          <h2>" . $row['company_name'] . "</h2>
                      </div>

                      <div class='box'>
                          " . ((empty($row['address_1'])) ? '' : "<strong> Address: </strong>" . $row['address_1']) . ((empty($row['address_2'])) ? '' : ",&nbsp" . $row['address_2']) . ((empty($row['town'])) ? '' : ",&nbsp" . $row['town']) . ((empty($row['county'])) ? '' : ",&nbsp" . $row['county']) . ((empty($row['postcode'])) ? '' : ",&nbsp" . $row['postcode']) . "
                      </div>

                      <div class='box'>
                          " . ((empty($row['tel'])) ? '' : "<strong> Telephone: </strong>" . $row['tel']) . ((empty($row['mobile'])) ? '' : "<strong> Mobile: </strong>" . $row['mobile']) . "
                      </div>

                  </div>

                  <div class='rightbox'>

                          <div class='service-pic'>
                           <img src='img/buddy.png' class='hvr-bob'>
                          </div>

                      <div id='map" . $counter . "' style='width: 250px; height: 250px;'></div>

                  </div>

                  </div><br/>";

              $jsInitFunction .= "var address" . $counter . " = '" . $row['address_1'] . "," . $row['town'] . "," . $row['postcode'] .", UK';

                                 var map" . $counter . " = new google.maps.Map(document.getElementById('map" . $counter .  "'), {
                                      mapTypeId: google.maps.MapTypeId.TERRAIN,
                                      zoom: 6
                                 });

                                 geocoder.geocode({
                                    'address': address" . $counter . "
                                 }, function(results, status) {
                                      if(status == google.maps.GeocoderStatus.OK) {
                                          new google.maps.Marker({
                                              position: results[0].geometry.location,
                                              map: map" . $counter . "
                                          });
                                          map" . $counter .  ".setCenter(results[0].geometry.location);
                                    }
                                });";

              $counter++;
          }
          $jsInitFunction .= "}";
    ?>

    <script>
      <?php print $jsInitFunction; ?>
    </script>


您正在javascript中打印php变量,但在
var address
@AlbertAkki处没有php标记。我如何更正此问题?请尝试var address=“,,UK”@AlbertAkki我试过了,谷歌地图在while循环中只显示一次。你能在pastebin中粘贴你的浏览器源代码(CTRL+U)吗?这样它就能更好地理解到底是什么问题。如果有任何错误,请发布控制台错误谢谢,我会试试这个。我已经实现了代码,但我似乎遇到了500个错误,似乎不明白为什么我发布了完整的代码。请看第131-133行。不应该
}
呆在
里面吗?我已经纠正了那个错误,但是我仍然得到了同样的错误。肯定是另一个问题,我已经更新了代码。好的。当
循环时关闭
,当
循环时关闭位于
之前的
。如果(isset($\u GET['k'],$\u GET['t']){
似乎未关闭,那么关闭
怎么样。