首页 > 解决方案 > 如何在模态引导程序 4 中显示谷歌地图

问题描述

我会在模态中显示谷歌地图而不使用 API。在循环内部,坐标可以改变(我只为这个例子写了一个常数)我试过这个。模态出现但不是地图。

谢谢

我的循环

 $i = 0;
        foreach ($result as $value) {
          $geolocalisation = '
          <a data-toggle="modal" data-target="#GeoModal'. $i .'">' . Core::getDef('modules_test') . '</a> |
          <div class="modal fade" id="GeoModal'. $i .'" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
              <div class="modal-content">-----
               <div id="mapCanvas" style="width: 500px; height: 400px"></div>
              </div>
            </div>
          </div>

          ';

脚本必须显示地图

 $geolocalisation .= '
<script>
            function initialize() {
  var mapOptions = {
    center: new google.maps.LatLng(51.219987, 4.396237),
    zoom: 12,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("mapCanvas"),
    mapOptions);
  var marker = new google.maps.Marker({
    position: new google.maps.LatLng(51.219987, 4.396237)
  });
  marker.setMap(map);
}
</script>
';

          $methods[]= ['id'     => $value->id,
                       'title'  => '<br> ' . $value->name . ' ' .  $value->address . ' ' .  $value->addressOptional . ' ' .  $value->locality. ' '  .  $value->city . ' ' .  $value->postalCode . ' ' .  $value->partialClosed . ' ' .  $geolocalisation,
                       'cost'   => 5.50
                      ];
$i++;
        }

标签: javascriptphpgoogle-mapsbootstrap-4

解决方案


您需要在按钮单击以打开模式时初始化地图并创建函数以使用包含您的纬度和经度位置的参数来初始化地图

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script async defer
    src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
    </script>
 <style>
       /* Set the size of the div element that contains the map */
      #map {
        height: 400px;  /* The height is 400 pixels */
        width: 100%;  /* The width is the width of the web page */
       }
    </style>
</head>
<body>

<div class="container">
  <h2>Modal Example</h2>
  <!-- Button to Open the Modal -->
  <!-- add attribute data-lat="5.134515" data-long="97.151759" to pass location data -->
  <button type="button" id="open" class="btn btn-primary" data-toggle="modal" data-target="#myModal" data-lat="5.134515" data-long="97.151759">
    Open modal
  </button>

  <!-- The Modal -->
  <div class="modal" id="myModal">
    <div class="modal-dialog">
      <div class="modal-content">

        <!-- Modal Header -->
        <div class="modal-header">
          <h4 class="modal-title">Modal Heading</h4>
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>

        <!-- Modal body -->
        <div class="modal-body">
           <!-- show map here -->
            <div id="map"></div>
        </div>

        <!-- Modal footer -->
        <div class="modal-footer">
          <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
        </div>

      </div>
    </div>
  </div>

</div>
<script>
   function init(myLoc) {
        var marker = new google.maps.Marker({
            position: myLoc
        });
      var opt = {
            center: myLoc,
            zoom: 13,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("map"), opt);
        marker.setMap(map);
    };

$("#open").click(function(){
// get latitude and longitude that pass from open modal button
    init(new google.maps.LatLng($(this).data('lat'), $(this).data('long')));
    });

</script>
</body>
</html>

推荐阅读