Javascript 如何在modal bootstrap 4中显示google地图

Javascript 如何在modal bootstrap 4中显示google地图,javascript,php,google-maps,bootstrap-4,Javascript,Php,Google Maps,Bootstrap 4,我想在模式中显示一个谷歌地图,而不用使用api。 在循环内部,coordonae可以改变(在这个例子中,我只写了一个常量) 我试过这个。模态显示,但不显示贴图 多谢各位 我的回路 $i = 0; foreach ($result as $value) { $geolocalisation = ' <a data-toggle="modal" data-target="#GeoModal'. $i .'">' . Core::g

我想在模式中显示一个谷歌地图,而不用使用api。 在循环内部,coordonae可以改变(在这个例子中,我只写了一个常量) 我试过这个。模态显示,但不显示贴图

多谢各位

我的回路

 $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>

          ';
$i=0;
foreach(结果为$value){
$GeoLocalization=
'.Core::getDef('modules_test')。'|
-----
';
脚本必须显示地图

 $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++;
        }
$GeoLocalization.='
函数初始化(){
变量映射选项={
中心:新google.maps.LatLng(51.219987,4.396237),
缩放:12,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
var map=new google.maps.map(document.getElementById(“mapCanvas”),
地图选项);
var marker=new google.maps.marker({
位置:新google.maps.LatLng(51.219987,4.396237)
});
marker.setMap(map);
}
';
$methods[]=['id'=>$value->id,
“title'=>”
“.$value->name.”.$value->address.”.$value->address可选.“.$value->locality.”.$value->city.“.$value->postalCode.”..$value->partialClosed.“$geologisation, “成本”=>5.50 ]; $i++; }
你好吗?这是你的谷歌地图和你的引导4模式


引导示例
模态示例
开放模态
模态标题
&时代;
接近

你好吗?这是你的谷歌地图和你的引导4模式


引导示例
模态示例
开放模态
模态标题
&时代;
接近

您需要在按钮上单击init map以打开模式,并使用包含纬度和经度位置的参数创建函数以init map

<!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>

引导示例
/*设置包含映射的div元素的大小*/
#地图{
高度:400px;/*高度为400像素*/
宽度:100%;/*宽度是网页的宽度*/
}
模态示例
开放模态
模态标题
&时代;
接近
函数初始化(myLoc){
var marker=new google.maps.marker({
职位:myLoc
});
变量opt={
中心:myLoc,
缩放:13,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
map=new google.maps.map(document.getElementById(“map”),opt);
marker.setMap(map);
};
$(“#打开”)。单击(函数(){
//获取从打开模式按钮传递的纬度和经度
init(新的google.maps.LatLng($(this.data('lat'),$(this.data('long')));
});

您需要在按钮上单击init map以打开模式,并使用包含纬度和经度位置的参数创建函数以init map

<!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>

引导示例
/*设置包含映射的div元素的大小*/
#地图{
高度:400px;/*高度为400像素*/
宽度:100%;/*宽度是网页的宽度*/
}
模态示例
开放模态
模态标题
&时代;
接近
函数初始化(myLoc){
var marker=new google.maps.marker({
职位:myLoc
});
变量opt={
中心:myLoc,
缩放:13,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
map=new google.maps.map(document.getElementById(“map”),opt);
marker.setMap(map);
};
$(“#打开”)。单击(函数(){
//获取从打开模式按钮传递的纬度和经度
init(新的google.maps.LatLng($(this.data('lat'),$(this.data('long')));
});

此解决方案使用谷歌地图嵌入api,而不是谷歌地图javascript api。我要说的是,这并没有回答上面提到的问题,因为这段代码没有使用GoogleMapJavaScriptAPI。这更像是一种黑客行为。该解决方案使用谷歌地图嵌入api,而不是谷歌地图javascript api。我要说的是,这并没有回答上面提到的问题,因为这段代码没有使用GoogleMapJavaScriptAPI。这更像是一种黑客行为