javascript - 如何在 iframe 标签中添加谷歌地图

标签 javascript html css google-maps iframe

我想向用户显示 map 方向并且我想在弹出式 iframe 标记中显示此 map 。我们如何在 iframe 标记中显示此 map

Here is the link我想在我的代码中的 iframe 标签中显示它

我不知道为什么这不起作用。预先感谢您的伟大想法

// Get the modal
var modal = document.getElementById('id01');

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}
<style>
/* Full-width input fields */
input[type=text], input[type=password] {
    width: 100%;
    padding: 12px 20px;
    margin: 8px 0;
    display: inline-block;
    border: 1px solid #ccc;
    box-sizing: border-box;
}

/* Set a style for all buttons */
button {
    background-color: #4CAF50;
    color: white;
    padding: 14px 20px;
    margin: 8px 0;
    border: none;
    cursor: pointer;
    width: 100%;
}

/* Extra styles for the cancel button */
.cancelbtn {
    width: auto;
    padding: 10px 18px;
    background-color: #f44336;
}

/* Center the image and position the close button */
.imgcontainer {
    text-align: center;
    margin: 24px 0 12px 0;
    position: relative;
}

img.avatar {
    width: 40%;
    border-radius: 50%;
}

.container {
    padding: 16px;
}

span.psw {
    float: right;
    padding-top: 16px;
}

/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
    padding-top: 10px;
}

/* Modal Content/Box */
.modal-content {
    background-color: #fefefe;
    margin: 5% auto 15% auto; /* 5% from the top, 15% from the bottom and centered */
    border: 1px solid #888;
    width: 80%; /* Could be more or less, depending on screen size */
height:auto;
}

/* The Close Button (x) */
.close {
    position: absolute;
    right: 25px;
    top: 0;
    color: #000;
    font-size: 35px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: red;
    cursor: pointer;
}

/* Add Zoom Animation */
.animate {
    -webkit-animation: animatezoom 0.6s;
    animation: animatezoom 0.6s
}

@-webkit-keyframes animatezoom {
    from {-webkit-transform: scale(0)} 
    to {-webkit-transform: scale(1)}
}
    
@keyframes animatezoom {
    from {transform: scale(0)} 
    to {transform: scale(1)}
}

/* Change styles for span and cancel button on extra small screens */
@media screen and (max-width: 300px) {
    span.psw {
       display: block;
       float: none;
    }
    .cancelbtn {
       width: 100%;
    }
}
</style>
<button onclick="document.getElementById('id01').style.display='block'" style="width:auto;">View Map Direction</button>

<div id="id01" class="modal">
  
  <form class="modal-content animate" action="action_page.php">
    <div class="imgcontainer">
      <span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">&times;</span>
      
    </div>

    <iframe src="https://www.google.com/maps/dir/31.5282618,74.3234164/University+of+South+Asia,+47+Tufail+Rd,+Lahore/@31.5308165,74.3164608,13z/data=!3m1!4b1!4m10!4m9!1m1!4e1!1m5!1m1!1s0x3919050d90b4fac3:0xa46dbb2def95e45a!2m2!1d74.3781741!2d31.5328815!3e"></iframe>

    <div class="container" style="background-color:#f1f1f1">
      <button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">Cancel</button>
   
    </div>
  </form>
</div>

最佳答案

在您的情况下,您正尝试在 iframe 中加载 map ,而 iframs 的“X-Frame-Option”设置为“SAMEORIGIN”。所以它不会加载到 iframe 中。 您不能在 iframe 上设置 X-Frame-Options。这是由您请求资源的域(在您的示例中为 www.google.com)设置的响应 header 。在这种情况下,他们将 header 设置为 SAMEORIGIN,这意味着他们不允许在域外的 iframe 中加载资源。有关详细信息,请参阅 X-Frame-Options response header在 MDN 上。

您可以使用以下链接在 iframe 中添加 map : How to load a Google Map in an iframe with Javascript?

关于javascript - 如何在 iframe 标签中添加谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41343080/

相关文章:

html - CSS - 背景颜色在 IE11 中不起作用

javascript - 加载到模态时,具体化选项卡不起作用

javascript - 嵌入不带任何 Logo 或 youtube 链接的 youtube 视频

javascript - 下面的 React 代码有什么区别?哪一种是最有效率或最有效的?或者它们实际上是相同的吗?

javascript - 提交后联系 form7 数据插入到不同的数据库表中

javascript - 为什么 HTML/JavaScript 小部件没有显示在我的 blogspot 中?

html - 如何在有序的 HTML 列表中为罗马数字加下划线

css - 用于提供静态文件的中间件不适用于动态路由

javascript - 在 JavaScript 中处理大型(12K+ 行)数组

javascript - 为什么这个 MongoDB 循环执行的次数比预期多?