我有一个带有 Google 地图混搭的页面,其中的图钉按日期(周一、周二等)进行颜色编码。包含地图的 IFrame 是动态调整大小的,因此当浏览器窗口调整大小时,它也会调整大小。

我想在地图窗口的一角放置一个图例,告诉用户每种颜色的含义。Google 地图 API 包括 GScreenOverlay 具有我想要的行为的类,但它只允许您指定要用作叠加层的图像,并且我更喜欢使用其中包含文本的 DIV。将 DIV 放置在地图窗口(例如)左下角的最简单方法是什么,当调整浏览器窗口大小时,该 DIV 会自动保持在相对于角的同一位置?

有帮助吗?

解决方案

您可以添加自己的自定义控件并将其用作图例。

此代码将添加一个 150w x 100h 的框(灰色边框/白色背景)以及其中的单词“Hello World”。您可以将图例中的文本替换为您想要的任何 HTML。这将保持锚定在地图的右上角 (G_ANCHOR_TOP_RIGHT) 下方 10 像素和上方 50 像素处。

function MyPane() {}
MyPane.prototype = new GControl;
MyPane.prototype.initialize = function(map) {
  var me = this;
  me.panel = document.createElement("div");
  me.panel.style.width = "150px";
  me.panel.style.height = "100px";
  me.panel.style.border = "1px solid gray";
  me.panel.style.background = "white";
  me.panel.innerHTML = "Hello World!";
  map.getContainer().appendChild(me.panel);
  return me.panel;
};

MyPane.prototype.getDefaultPosition = function() {
  return new GControlPosition(
      G_ANCHOR_TOP_RIGHT, new GSize(10, 50));
      //Should be _ and not _
};

MyPane.prototype.getPanel = function() {
  return me.panel;
}
map.addControl(new MyPane());

其他提示

我会使用如下 HTML:

<div id="wrapper">
   <div id="map" style="width:400px;height:400px;"></div>
   <div id="legend"> ... marker descriptions in here ... </div>
</div>

然后,您可以设置此样式以将图例保留在右下角:

div#wrapper { position: relative; }
div#legend { position: absolute; bottom: 0px; right: 0px; }

position: relative 将导致任何包含的元素相对于 #wrapper 容器,以及 position: absolute 会导致 #legend div 被“拉”出流程并位于地图上方,使其右下边缘保持在地图的底部 #wrapper 并根据需要进行拉伸以包含标记描述。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top