/* An InfoBox is like an info window, but it displays
* under the marker, opens quicker, and has flexible styling.
* @param {GLatLng} latlng Point to place bar at
* @param {Map} map The map on which to display this InfoBox.
* @param {Object} opts Passes configuration options - content,
* offsetVertical, offsetHorizontal, className, height, width
*/
function InfoBox(opts) {
google.maps.OverlayView.call(this);
this.latlng_ = opts.latlng;
this.map_ = opts.map;
this.offsetVertical_ = -195;
this.offsetHorizontal_ = 0;
this.height_ = 165;
this.width_ = 266;
var me = this;
this.boundsChangedListener_ =
google.maps.event.addListener(this.map_, "bounds_changed", function() {
return me.panMap.apply(me);
});
// Once the properties of this OverlayView are initialized, set its map so
// that we can display it. This will trigger calls to panes_changed and
// draw.
this.setMap(this.map_);
}
之后它继续覆盖GOverlay:
InfoBox.prototype = new google.maps.OverlayView();
google.maps.event.addListener(popup, 'domready', function() {
var l = $('#hook').parent().parent().parent().siblings();
for (var i = 0; i < l.length; i++) {
if($(l[i]).css('z-index') == 'auto') {
$(l[i]).css('border-radius', '16px 16px 16px 16px');
$(l[i]).css('border', '2px solid red');
}
}
});
您可以执行任何操作,例如设置新的CSS类或只添加新元素 .
玩弄元素来获得你需要的东西......
34
google.maps.event.addListener(infowindow, 'domready', function() {
// Reference to the DIV that wraps the bottom of infowindow
var iwOuter = $('.gm-style-iw');
/* Since this div is in a position prior to .gm-div style-iw.
* We use jQuery and create a iwBackground variable,
* and took advantage of the existing reference .gm-style-iw for the previous div with .prev().
*/
var iwBackground = iwOuter.prev();
// Removes background shadow DIV
iwBackground.children(':nth-child(2)').css({'display' : 'none'});
// Removes white background DIV
iwBackground.children(':nth-child(4)').css({'display' : 'none'});
// Moves the infowindow 115px to the right.
iwOuter.parent().parent().css({left: '115px'});
// Moves the shadow of the arrow 76px to the left margin.
iwBackground.children(':nth-child(1)').attr('style', function(i,s){ return s + 'left: 76px !important;'});
// Moves the arrow 76px to the left margin.
iwBackground.children(':nth-child(3)').attr('style', function(i,s){ return s + 'left: 76px !important;'});
// Changes the desired tail shadow color.
iwBackground.children(':nth-child(3)').find('div').children().css({'box-shadow': 'rgba(72, 181, 233, 0.6) 0px 1px 6px', 'z-index' : '1'});
// Reference to the div that groups the close button elements.
var iwCloseBtn = iwOuter.next();
// Apply the desired effect to the close button
iwCloseBtn.css({opacity: '1', right: '38px', top: '3px', border: '7px solid #48b5e9', 'border-radius': '13px', 'box-shadow': '0 0 5px #3990B9'});
// If the content of infowindow not exceed the set maximum height, then the gradient is removed.
if($('.iw-content').height() < 140){
$('.iw-bottom-gradient').css({display: 'none'});
}
// The API automatically applies 0.7 opacity to the button after the mouseout event. This function reverses this event to the desired value.
iwCloseBtn.mouseout(function(){
$(this).css({opacity: '1'});
});
});
7 回答
谷歌写了一些代码来帮助解决这个问题 . 以下是一些示例:Example using InfoBubble,Styled markers和Info Window Custom(使用OverlayView) .
上面链接中的代码采用不同的路由来实现类似的结果 . 它的要点是直接设置InfoWindows的样式并不容易,使用额外的InfoBubble类而不是InfoWindow或覆盖GOverlay可能更容易 . 另一个选择是使用javascript(或jQuery)修改InfoWindow的元素,就像后来ATOzTOA建议的那样 .
可能这些示例中最简单的是使用InfoBubble而不是InfoWindow . 通过导入此文件(您应该自己托管)可以使用InfoBubble:
http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobubble/src/infobubble.js
InfoBubble's Github project page .
与InfoWindow相比,InfoBubble非常具有风格:
您也可以使用给定的 Map 和标记来打开它:
另一个例子是,Info Window Custom示例从Google Maps API扩展了GOverlay类,并将其用作创建更灵活的信息窗口的基础 . 它首先创建类:
之后它继续覆盖GOverlay:
然后,您应该覆盖所需的方法:
createElement
,draw
,remove
和panMap
. 它变得相当复杂,但理论上你现在只是自己在 Map 上画一个div,而不是使用普通的信息窗口 .您可以单独使用jquery修改整个InfoWindow ...
这里<p>元素将充当实际InfoWindow的钩子 . 一旦domready触发,该元素将变为活动状态并可使用javascript / jquery访问,如
$('#hook').parent().parent().parent().parent()
.下面的代码只是在InfoWindow周围设置了一个2像素的边框 .
您可以执行任何操作,例如设置新的CSS类或只添加新元素 .
玩弄元素来获得你需要的东西......
// CSS放入样式表
我使用以下代码来应用一些外部CSS:
使用Google Maps Utility Library中的 InfoBox 插件 . 它使得样式/管理 Map 弹出窗口变得更加容易 .
请注意,您需要确保在Google Maps API之后加载它:
我有设计谷歌 Map infowindow与图像和一些内容如下 .
你也可以使用css类 .
美好的一天!