修复了 Google 地图混搭中的图例

发布于 2024-07-04 18:26:24 字数 430 浏览 10 评论 0原文

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

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

I have a page with a Google Maps mashup that has pushpins that are color-coded by day (Monday, Tuesday, etc.) The IFrame containing the map is dynamically sized, so it gets resized when the browser window is resized.

I'd like to put a legend in the corner of the map window that tells the user what each color means. The Google Maps API includes a GScreenOverlay class that has the behavior that I want, but it only lets you specify an image to use as an overlay, and I'd prefer to use a DIV with text in it. What's the easiest way to position a DIV over the map window in (for example) the lower left corner that'll automatically stay in the same place relative to the corner when the browser window is resized?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愁杀 2024-07-11 18:26:24

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

此代码将添加一个 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());

You can add your own Custom Control and use it as a legend.

This code will add a box 150w x 100h (Gray Border/ with White Background) and the words "Hello World" inside of it. You swap out the text for any HTML you would like in the legend. This will stay Anchored to the Top Right (G_ANCHOR_TOP_RIGHT) 10px down and 50px over of the map.

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());
手心的海 2024-07-11 18:26:24

我将使用如下所示的 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 定位code> 容器,并且 position:absolute 将导致 #legend div 被“拉”出流程并位于地图上方,使其右下边缘保持在#wrapper 的底部并根据需要进行拉伸以包含标记描述。

I would use HTML like the following:

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

You can then style this to keep the legend in the bottom right:

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

position: relative will cause any contained elements to be positioned relative to the #wrapper container, and position: absolute will cause the #legend div to be "pulled" out of the flow and sit above the map, keeping it's bottom right edge at the bottom of the #wrapper and stretching as required to contain the marker descriptions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文