javascript - 如何在模态引导程序 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++;
}
解决方案
您需要在按钮单击以打开模式时初始化地图并创建函数以使用包含您的纬度和经度位置的参数来初始化地图
<!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">×</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>
推荐阅读
- javascript - 样式化组件:悬停适用于反应图标,但不适用于自定义 svg 图标
- excel - 通过 Power 查询在 Excel 中下载表格
- python - 如何在python中将二进制线扩展到图像的边界?
- git - 有没有 GItHub 网络图的 api
- sql - 如何根据来自同一个表的唯一值加入两次在 postgres 中创建数组?
- javascript - Mongoose - CastError Cast to string failed for value "Object"
- c# - 在 C# winForm 中使用数据字典匹配列
- azure-ad-b2c - 如何在不注入 B2C 生成的表单控件的情况下自定义 Azure B2C 模板
- yugabyte-db - 在 YugabyteDB YSQL 中分片时如何对列进行哈希处理
- kotlin - 从两个可变集合检查不重叠的元素 - kotlin