【问题标题】:Google Maps Embed API Not Centering Pins谷歌地图嵌入 API 不居中引脚
【发布时间】:2014-07-28 03:56:22
【问题描述】:

我正在构建一个使用 Google 的 Mapping API Embed V1.0 的网站,并且我指定的坐标显示正确,但是该点不在映射视图的中心。

问题照片:

iFrame 代码:

<iframe class="sectionMap" width="300" height="300" frameborder="0" style="border:0" src="https://www.google.com/maps/embed/v1/place?key=AIzaSyAGJVo8xnxXbEICl5IuDAbmWIBsE0cFKVQ&amp;q=42.3599,-71.0655&amp;zoom=18&amp;maptype=satellite"></iframe>

CSS:

.sectionMap{
/*
    width: 300px;
    height: 300px;
*/
    display: block;
    position: relative;
    left: 50%;
    margin-left: -150px;
    border-style: solid;
    border-width: 2px;
    box-shadow: 1px 1px 5px 3px #ccc;
}

这是来自网络应用程序的示例页面:

<section id="williamMonroeTrotterHouse" data-role="page" data-theme="a">
    <header data-role="header" data-position="fixed" data-id="appHeader"><a href="#landmarks" data-transition="slide" data-direction="reverse" data-icon="arrow-l" data-rel="back" data-theme="a">Back</a>
        <h1>William Monroe Trotter House</h1>
    </header>
    <div data-role="content">
        <p><span class="sectionTitle">Location: </span>97 Sawyer Ave., Dorchester</p>
        <p><span class="sectionTitle">Description: </span>Home of African-American journalist and Harvard graduate William Monroe Trotter. Trotter publisher The Guardian, and meetings of African-American activists, W. E. B. Du Bois among them, took place at this house.</p>
        <img src="Landmark_Photos/WIlliam_Monroe_Trotter_House.jpg" class="sectionPhoto">
        <br>
        <iframe class="sectionMap" width="300" height="300" frameborder="0" style="border:0" src="https://www.google.com/maps/embed/v1/place?key=AIzaSyAGJVo8xnxXbEICl5IuDAbmWIBsE0cFKVQ&amp;q=42.3127,-71.0624&amp;zoom=18&amp;maptype=satellite"></iframe>
    </div>
</section>

任何想法为什么这不是居中?

PS:我不认为这是一个重复的帖子,因为 Google 最近更新了映射 API,并且之前问题的答案不起作用。

【问题讨论】:

  • 什么设置 iframe 的大小?
  • 感谢您的回复:宽度:300px;高度:250px;
  • 似乎对我有用:jsfiddle.net/teddyrised/XADL4
  • 看不到假定的“兴趣点”在哪里。左上角?
  • 这很奇怪。在 JS Fiddle 上,它显示在中间,但在 Safari 上却不是。这会不会与 JQuery Mobile 冲突?

标签: html css google-maps iframe


【解决方案1】:

在我的 Safari 版本 (5.1.7) 中,完全由您编写代码的小提琴可以正常工作。哪个版本有问题?我能想象的唯一原因是位置和边距的“棘手”居中。我会尝试margin:0 auto; 进行居中,没有相对位置、顶部和边距。或者使用旧方法:display:inline-block 在带有text-align:center 的 div 中。恕我直言,最后一个是跨浏览器最多的。

.sectionMapWrapper{
width:100%;
text-align:center;
}

.sectionMap{
display: inline-block;
border-style: solid;
border-width: 2px;
box-shadow: 1px 1px 5px 3px #ccc;
}

编辑:

如果它没有解决问题,请检查您是否有某种库(或您自己的函数)可以使您的 iframe “显示:无”。

如果加载内容时没有布局(显示:无),则 iframe 脚本的大小为 0x0,中心位于 ... 0/x , 0/x。保持显示块直到加载内容,然后您可以使用 display:none 隐藏它。

在类似的情况下,我解决了这个问题,将 iframe 放置在一个 0x0 大小的 div 中,并隐藏了溢出(在样式属性处)。当确定 iframe 内容已加载时,会触发将 iframe 显示设置为 none 并删除包装器 div 样式属性的函数。 (或类似的,取决于您需要的其他行为)。希望对您有所帮助。

【讨论】:

  • 感谢您的回答,我正在运行 Safari 7.0.4 和 10.9.3 和 iOS 7 Safari。
  • @Jake 我编辑了答案。如果有“实时”版本,我想尽可能检查整个内容。
  • 好的,这里是测试版的网址:jakeserver.com/Apps/BostonLandmarks/B4 请点击任何标签(第一个有点错误),详细信息屏幕将显示谷歌地图。它是从 js/landmarks.js 生成的。感谢您查看它。
  • 它在 jquery.mobile-1.4.2.css 的第 1336 行。那是显示没有使这件事发生。整个部分都有显示:无。希望您可以根据自己的需要调整 anwser 中的工作区。试着注释掉这个“display:none”,也许这就是你所要做的。
  • ...并且加载所有这些 iframe 并不是一个好主意,因为“是”。我会等待用户点击插入每一个。这解决了这两个问题。考虑一下您可以浪费多少带宽让移动用户加载所有他可能永远不会看到的地图。
猜你喜欢
  • 1970-01-01
  • 2014-04-27
  • 1970-01-01
  • 2013-10-17
  • 2012-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-27
相关资源
最近更新 更多