更改 Google 地图信息窗口关闭图标

发布于 2024-08-08 06:32:25 字数 25 浏览 4 评论 0原文

如何更改信息窗口关闭按钮的默认图形?

How can I change the default graphic for the Infowindow close button?

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

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

发布评论

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

评论(8

假装爱人 2024-08-15 06:32:25

您可以使用 CSS 来完成此操作,如下所示:

img[src="http://maps.gstatic.com/intl/en_us/mapfiles/iw_close.gif"] 
{ content:url("/img/own_close_button.png"); }

适用于:

  • Chrome 14.0.835.163
  • Safari 4.0.5
  • Opera 10.6
  • 大多数移动设备(默认浏览器)

不适用于:

  • FireFox 27.0
  • IE 11.0

You can do it with CSS, like this:

img[src="http://maps.gstatic.com/intl/en_us/mapfiles/iw_close.gif"] 
{ content:url("/img/own_close_button.png"); }

Works on:

  • Chrome 14.0.835.163
  • Safari 4.0.5
  • Opera 10.6
  • Most mobile devices (default browser)

Does not work on:

  • FireFox 27.0
  • IE 11.0
还不是爱你 2024-08-15 06:32:25

如果您使用的是谷歌地图 API V3,那么您可能需要使用官方添加的信息框。
Google 地图实用程序 - 信息框< /a>

If you're using google maps API V3, then you might want to use the official add on infobox.
Google Maps Utility - Infobox

半窗疏影 2024-08-15 06:32:25

首先,您需要隐藏默认的关闭图标:

.gm-style-iw button.gm-ui-hover-effect img {
    display: none !important;
}

其次,强制默认按钮(保存图像)具有完全不透明度:

.gm-style-iw button.gm-ui-hover-effect {
  opacity: 1 !important;
}

第三,设置您自己的图像并定位它:

.gm-style-iw button.gm-ui-hover-effect:before {
  display: block;
  content: "";
  background: url('assets/close-red.svg') center center no-repeat;
  background-size: cover;
  width: 8px;
  height: 8px;
  right: -19px;
  position: relative;
}

结果:

custom-close-button

First you need to hide the default close icon:

.gm-style-iw button.gm-ui-hover-effect img {
    display: none !important;
}

Second, force the default button (that holds the image) to have full opacity:

.gm-style-iw button.gm-ui-hover-effect {
  opacity: 1 !important;
}

Third, set your own image and position it:

.gm-style-iw button.gm-ui-hover-effect:before {
  display: block;
  content: "";
  background: url('assets/close-red.svg') center center no-repeat;
  background-size: cover;
  width: 8px;
  height: 8px;
  right: -19px;
  position: relative;
}

Result:

custom-close-button

使用 jQuery,您可以更改图像文件位置。如果我有一个大小为 16px 的图像button_close.png:

jQuery('img[src="http://maps.gstatic.com/intl/en_us/mapfiles/iw_close.gif"]').livequery(function() {
    jQuery(this).attr('width', 16).attr('height', 16).css({
        width: '16px',
        height: '16px'
    }).attr('src', 'button_close.png');
});

当然,只有当 Google 使用此文件位置时,这才有效。

With jQuery you can change the image file location. If I have an image button_close.png that is 16px in size:

jQuery('img[src="http://maps.gstatic.com/intl/en_us/mapfiles/iw_close.gif"]').livequery(function() {
    jQuery(this).attr('width', 16).attr('height', 16).css({
        width: '16px',
        height: '16px'
    }).attr('src', 'button_close.png');
});

Of course this only works as long as Google uses this file location.

月竹挽风 2024-08-15 06:32:25
button.gm-ui-hover-effect {
background: url(your-img.png) !important;
background-size: contain !important;
}

button.gm-ui-hover-effect img {
display: none !important;
}
button.gm-ui-hover-effect {
background: url(your-img.png) !important;
background-size: contain !important;
}

button.gm-ui-hover-effect img {
display: none !important;
}
走过海棠暮 2024-08-15 06:32:25

使用Dimitrije Djekanovic授予 建议,这是当前 API 版本的工作版本 3.49(2022 年 5 月中旬):

/* hides the x image */
button.gm-ui-hover-effect > span {
  display: none !important;
}

/* inserts the customized and clickable image instead */
button.gm-ui-hover-effect {
  opacity: 1 !important;
  background: url('close.svg') center center !important;
  background-repeat: no-repeat !important;
  background-size: 16px 16px !important;
  /* above is a good size or uncomment the next line */
  /* background-size: contain !important; */
}

要对此进行测试,请转到 StackBlitz 演示,由 官方文档并粘贴style.css文件,同时将background更改为:

background: url('https://upload.wikimedia.org/wikipedia/commons/thumb/5/51/Mr._Smiley_Face.svg/414px-Mr._Smiley_Face.svg.png')
    center center !important;

output

Using what Dimitrije Djekanovic and Grant suggested, here is a working version for the current API version 3.49 (Mid-May of 2022):

/* hides the x image */
button.gm-ui-hover-effect > span {
  display: none !important;
}

/* inserts the customized and clickable image instead */
button.gm-ui-hover-effect {
  opacity: 1 !important;
  background: url('close.svg') center center !important;
  background-repeat: no-repeat !important;
  background-size: 16px 16px !important;
  /* above is a good size or uncomment the next line */
  /* background-size: contain !important; */
}

To test this, go to the StackBlitz demo provided by the official documentation and paste the style.css file while having background changed to:

background: url('https://upload.wikimedia.org/wikipedia/commons/thumb/5/51/Mr._Smiley_Face.svg/414px-Mr._Smiley_Face.svg.png')
    center center !important;

output

揪着可爱 2024-08-15 06:32:25

确实没有办法做到这一点。您最好的选择可能是使用第三方或自定义信息窗口。

此处列出了一些第三方解决方案。

There isn't really a way to do this. Your best bet would probably be to use a third-party or custom Infowindow.

There's a list of some third-party solutions here.

只为一人 2024-08-15 06:32:25

default icon 替换为 “关闭图标”(或任何您可能喜欢的图像),之后

infowindow.open(map, marker);

您可以尝试添加这些行

$('img[src="http://maps.gstatic.com/intl/en_us/mapfiles/iw_close.gif"]').each(function() {
    $(this).attr('width', 14);
    $(this).attr('height', 13);
    $(this).css({width: '14px',height: '13px'});
    $(this).attr('src','http://www.google.com/intl/en_us/mapfiles/close.gif');
});

To replace default icon with close icon (or whatever image you might like), after

infowindow.open(map, marker);

you can try to add these lines

$('img[src="http://maps.gstatic.com/intl/en_us/mapfiles/iw_close.gif"]').each(function() {
    $(this).attr('width', 14);
    $(this).attr('height', 13);
    $(this).css({width: '14px',height: '13px'});
    $(this).attr('src','http://www.google.com/intl/en_us/mapfiles/close.gif');
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文