javascript - Google Maps API 如何在 iframe 中隐藏 UI

标签 javascript google-maps google-maps-api-3

我得到了我的 API key ,并像这样构造了我的嵌入代码:

<iframe
    id="map-canvas"
    width="450"
    height="250"
    frameborder="0" style="border:0"
    src="https://www.google.com/maps/embed/v1/view?key=MYKEY&center=LAT,LNG&zoom=12&maptype=roadmap" allowfullscreen>
</iframe>

到目前为止一切顺利。 现在,我试图隐藏 map 的默认用户界面。 Google 的文档为我提供了这段代码:

function initialize() {
var mapOptions = {
    zoom: 4,
    center: new google.maps.LatLng(-33, 151),
    disableDefaultUI: true
  };
  var map = new google.maps.Map(document.getElementById('map-canvas'),
                                mapOptions);
}

google.maps.event.addDomListener(window, 'load', initialize);

令我困惑的是,这段代码试图再次创建 map ,但我的代码已经显示为 <iframe>上面的标记。我试过添加 map-canvas id 到我的 iframe,但脚本不会影响它。

最佳答案

您可以通过在 html 中添加空 div 来简单地创建 map :

<div id="map-canvas"></div>

然后就可以通过JS初始化了:

function initialize() {
  var mapOptions = {
    zoom: 4,
    center: new google.maps.LatLng(-33, 151),
    disableDefaultUI: true
  };

  // Create new map object (gets element by id #map-canvas)
  var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}

google.maps.event.addDomListener(window, 'load', initialize);

通过JS初始化 map 时无需添加iframe。

如果您希望完全禁用 Google map 用户界面,请查看:https://developers.google.com/maps/documentation/javascript/examples/control-disableUI

jsbin-http://jsbin.com/kudixegayo/1/edit?html,css,js,output

关于javascript - Google Maps API 如何在 iframe 中隐藏 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31301624/

相关文章:

javascript - 加载页面上带有 Canvas 的 XMLHttpRequest

javascript - 重定位一个 div

javascript - 单击谷歌地图标记时运行脚本

javascript - 如何在谷歌地图图钉上添加文本标签?

javascript - Google map V3 中的 Elabel 未定义错误

javascript - Google map JS API V3,添加多个标记以与来自远程 json 源的 infowindows 进行映射

javascript - 如何在 JavaScript 中强制使用分号?

javascript - 上下文菜单的宽度

javascript - 使用 QtWebKit 在桌面应用程序中检索 google maps v3 标记到 Qt 的位置

javascript - Google Maps API - 如果找不到一个地址,请尝试另一个地址