提问人:Andrew G. Johnson 提问时间:10/31/2011 最后编辑:QuantumAndrew G. Johnson 更新时间:11/8/2022 访问量:21658
我可以在 Google Maps API v3 中仅删除 POI 的弹出气泡吗?
Can I remove just the popup bubbles of POI's in Google Maps API v3?
问:
因此,我正在开发一个新的 Web 应用程序,该应用程序非常关注地图。使用 Google Maps API v3 并且对它非常满意,但注意到兴趣点 (POI) 会自动冒泡,其中包含更多详细信息和指向 Google 地方页面的链接。我不想要这些。这是我的代码:
map = new google.maps.Map(document.getElementById("map"), {
center:new google.maps.LatLng(default_latitude,default_longitude),
zoom:11,
mapTypeId:google.maps.MapTypeId.ROADMAP,
mapTypeControl:false,
panControl:false
});
我知道您可以完全删除 POI。这是我的代码:
map = new google.maps.Map(document.getElementById("map"),{
center:new google.maps.LatLng(default_latitude,default_longitude),
zoom:11,
mapTypeId:google.maps.MapTypeId.ROADMAP,
mapTypeControl:false,
panControl:false,
styles:[{
featureType:"poi",
elementType:"labels",
stylers:[{
visibility:"off"
}]
}]
});
这完全消除了一切,我仍然希望看到标签,因为我认为它们带来了价值,但只是认为泡沫太分散注意力了。
作为参考,这里是我想删除的气泡:
这是完全删除 POI 的相同地图:
答:
如果 Google 不允许您通过 API 直接访问它们(我认为您应该能够访问它们),我会使用 firebug 检查元素并用于删除这些样式display:none !important;
评论
建议你看看我在这里给出的答案:如何删除默认标记?
var myStyles =[
{
featureType: "poi",
elementType: "labels",
stylers: [
{ visibility: "off" }
]
}
];
var myOptions = {
zoom: 10,
center: homeLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
styles: myStyles
};
编辑:好的,看起来谷歌实施了一个解决方案,看看xomena的答案。
好的,在到处搜索之后,看起来您不能只在禁用点击的情况下显示 POI,您可以在此处查看更多讨论:
特别是这种交流:
你好
是否有可能使 POI 可见但不可点击?
谢谢 洛伦佐
克里斯·布罗德富特
不幸的是,不是洛伦佐。您需要应用地图样式才能隐藏 POI 标签:
[ { featureType: “poi”, elementType: “labels”, stylers: [ { 可见性: “关闭” } ] } ]
(或者只是隐藏业务 poi,“poi.business”)
这来自 Google 地图开发人员,因此这意味着您无法禁用弹出窗口,只能禁用 POI。
评论
找到解决方法!它很脏,所以如果谷歌改变了一些东西,它可能会停止工作,但它有效!
您必须找到 google 放置 infoWindows for POI 的层。幸运的是,这些层与用于用户 infoWindows 的层不同。诀窍是找到合适的层。阴影层可以很容易地找到,但 infoWindow 层是在创建一些 POI infoWindow 之后创建的,因此您必须像 google 一样侦听同一 div 上的点击事件。然后,您可以通过 z-index 或图像 url 找到 POI infoWindow 图层,但这还没有经过很好的测试......请注意,如果谷歌更改了 z-index,它将停止工作......
var lis = google.maps.event.addListener(my_map, 'tilesloaded', function () {
$('*').filter(function () { return $(this).css('z-index') == 104 }).remove();
// remove layer for POI infoWindow shadow - has z-index: 104
var eventDiv = $(my_map.getDiv()).children().children()[0];
// this div is used by google to handle events
$(eventDiv).click(function clickHandler() {
var timeout = 100;
var attempts = 20;
setTimeout(function timeoutHandler() {
// there are 2 ways how to find POI infoWindow layer
// 1st way - by the z-index
var poiInfoLayer = $('*').filter(function () {
return $(this).css('z-index') == 169 ||
$(this).css('z-index') == 248
});
// 2nd way - by the images :-)
// but not tested much - it may also find normal infoWindows!
//var poiInfoLayer = $('[src*="/mapfiles/iw3.png"]').parent().parent();
if (poiInfoLayer.length) {
poiInfoLayer.remove();
$(eventDiv).unbind('click', clickHandler);
}
else {
if (attempts > 0) {
setTimeout(timeoutHandler, timeout);
attempts--;
}
}
}, timeout);
});
google.maps.event.removeListener(lis);
});
评论
自 Google Maps API 更新以来,不再有效。
我终于找到了!
这是演示:http://jsfiddle.net/fbDDZ/14/(单击“打开”或POI不执行任何操作,单击“请打开”打开InfoWindow)
这个想法是修补 InfoWindow.prototype.open,以便允许它接受第三个参数。谷歌没有通过它,但我们应该通过。
代码:
function fixInfoWindow() {
var proto = google.maps.InfoWindow.prototype,
open = proto.open;
proto.open = function(map, anchor, please) {
if (please) {
return open.apply(this, arguments);
}
}
}
Google 在 POI 上打开 InfoWindow,如下所示:
myInfoWin.open(map, poiMarker)
窗口不会打开,因为谷歌没有说“请”。这就是我们应该打开信息窗口的方式:
myInfoWin.open(map, poiMarker, true);
评论
open
fixInfoWindow
编者注:这个答案一直适用到3.23版本。自 2016 年发布 3.24 版以来,您可以使用 clickableIcons
地图选项。请看xomena的回答。
版本 ~3.12 修复。演示
这是一个再次起作用的新补丁。我已经用 3.14 版对其进行了测试。
现在我们要打补丁而不是 .set()
open()
function fixInfoWindow() {
// Here we redefine the set() method.
// If it is called for map option, we hide the InfoWindow, if "noSuppress"
// option is not true. As Google Maps does not know about this option,
// its InfoWindows will not be opened.
var set = google.maps.InfoWindow.prototype.set;
google.maps.InfoWindow.prototype.set = function (key, val) {
if (key === 'map' && ! this.get('noSuppress')) {
console.warn('This InfoWindow is suppressed.');
console.log('To enable it, set "noSuppress" option to true.');
return;
}
set.apply(this, arguments);
}
}
您所要做的是将选项设置为您自己的选项以打开它们,例如:noSuppress
true
InfoWindow
var popup = new google.maps.InfoWindow({
content: 'Bang!',
noSuppress: true
});
popup.setPosition(new google.maps.LatLng(-34.397, 150.644));
popup.open(map);
艺术
var popup = new google.maps.InfoWindow({
content: 'Bang!',
});
popup.set('noSuppress', true);
popup.setPosition(new google.maps.LatLng(-34.397, 150.644));
popup.open(map);
评论
noSuppress
set
从版本 3.24 开始,Maps JavaScript API 在 MapOptions 对象中有一个属性 clickableIcons:
https://developers.google.com/maps/documentation/javascript/3.exp/reference#MapOptions
可以使用此属性通过将 clickableIcons 属性设置为 false 来关闭地图上的可单击图标。还存在一个 setClickableIcons() 方法。
请看这个例子: http://jsbin.com/liyamecoqa/edit?html,output
是的,总而言之,如果你想“删除”弹出窗口,你可以只使用:
clickableIcons: false
用于 Map Options 对象。但是,如果你想有一个弹出窗口,但没有“在谷歌地图上查看”标签,你可以使用一个笨拙的CSS技巧:
google-map {
.view-link {
display: none !important;
}
}
这是我的要求,而且很有效!
如果要定义哪些图标/图钉(元素或标签,无论您如何称呼它)应该可见,您可以使用此站点生成具有地图设置的正确 JSON: https://mapstyle.withgoogle.com/
评论
event.placeId
event.stop()