你可以创建一个custom overlay而不是一个标记
它将为您提供使用 HTML 的选项,通过 CSS 创建一个样式与您的示例图像类似的元素并不难。
一种可能的实现方式:
function HtmlMarker(map,position,content,cssText){
this.setValues({
position:position,
container:null,
content:content,
map:map,
cssText:cssText
});
this.onAdd = function() {
var that = this,container = document.createElement('div'),
content = this.get('content'),
cssText = this.get('cssText')
||'border-color:#fff;background:#fff;color:#000;';
container.className='HtmlMarker';
container.style.cssText = cssText;
google.maps.event.addDomListener(container,'click',
function(){google.maps.event.trigger(that,'click');});
if(typeof content.nodeName!=='undefined'){
container.appendChild(content);
}
else{
container.innerHTML=content;
}
container.style.position='absolute';
this.set('container',container)
this.getPanes().floatPane.appendChild(container);
}
this.draw=function(){
var pos = this.getProjection()
.fromLatLngToDivPixel(this.get('position')),
container = this.get('container');
container.style.left = pos.x - (container.offsetWidth/2)+ 'px';
container.style.top = pos.y - (container.offsetHeight) + 'px';
}
this.onRemove = function() {
this.get('container').parentNode.removeChild(this.get('container'));
this.set('container',null)
}
}
overlay-div 将具有 className HtmlMarker ,使用此类应用基本布局:
.HtmlMarker {
font-size:18px;
margin-top:-6px;/*!*/
padding: 4px 10px;
position: relative;
display:inline-block;
cursor:pointer;
}
.HtmlMarker:after {
position: absolute;
display: inline-block;
border-bottom: 6px solid transparent;/*!*/
border-top: 6px solid;/*!*/
border-top-color:inherit;
border-right: 6px solid transparent;/*!*/
border-left: 6px solid transparent;/*!*/
left: 50%;
margin-left:-6px;/*!*/
top:100%;
content:'';
}
属性中用/*!*/标记的6px/-6px定义了tip的大小,修改大小时必须修改所有这些值。
HtmlMarker() (map,position,content) 的前 3 个参数应该是不言自明的。
第 4 个参数 cssText 需要一个 cssText,它为您提供了应用自定义 CSS 的选项。
要定义背景颜色,您必须将background 和 border-color 设置为相同的值:例如:
border-color:white;background:white;
可以根据需要添加更多属性,但基本上可以使用样式表来应用更多样式(除了颜色,这个值当然要与背景兼容)
已经实现了点击事件,还可以实现更多功能,例如draggable-option。
演示:http://jsfiddle.net/doktormolle/5arST/