请看这里:
http://jsfiddle.net/nickaknudson/KVa2d/
此链接为JSFiddle上的一个代码示例。
tooltip = new Tooltip("text");
...
tooltip.open(map, marker);
可通过CSS进行自定义。
更新
有注释的代码:
http://jsfiddle.net/nickaknudson/KVa2d/12/
更新2
删除了不必要的部分:
http://jsfiddle.net/nickaknudson/KVa2d/14/
var Tooltip
Tooltip = function(tip) {
this.tip = tip;
this.buildDOM();
};
$.extend(Tooltip.prototype, google.maps.OverlayView.prototype, {
buildDOM: function() {
this.bdiv = $("<div></div>").addClass('WindowBody').html(this.tip);
this.wdiv = $("<div></div>").addClass('Window').append(this.bdiv);
this.sdiv = $("<div></div>").addClass('WindowShadow');
this.close();
},
onAdd: function() {
$(this.getPanes().floatPane).append(this.wdiv);
$(this.getPanes().floatShadow).append(this.sdiv);
},
onRemove: function() {
this.wdiv.detach();
this.sdiv.detach();
},
draw: function() {
var pos, left, top;
if (!this.getProjection()) return;
if (!this.get('position')) return;
pos = this.getProjection().fromLatLngToDivPixel(this.get('position'));
top = pos.y - this.getAnchorHeight() / 2;
if (this.getMap().getCenter().lng() > this.get('position').lng()) {
left = pos.x + this.wdiv.width() * 0.5;
} else {
left = pos.x - this.wdiv.width() * 1.5;
}
this.wdiv.css('top', top);
this.wdiv.css('left', left);
this.sdiv.css('top', (top - this.getAnchorHeight() / 2));
this.sdiv.css('left', left);
this.sdiv.width(this.wdiv.width());
this.sdiv.height(this.wdiv.height());
},
open: function(map, anchor) {
if (map) this.setMap(map);
if (anchor) {
this.set('anchor', anchor);
this.bindTo('anchorPoint', anchor);
this.bindTo('position', anchor);
}
this.draw();
this.wdiv.show();
this.sdiv.show();
this.isOpen = true;
},
close: function() {
this.wdiv.hide();
this.sdiv.hide();
this.isOpen = false;
},
getAnchorHeight: function() {
return -1 * this.get('anchorPoint').y;
}
});
更新 3
使用outerWidth()和outerHeight()更好地定位,可以考虑边框等因素。已删除阴影div。
http://jsfiddle.net/nickaknudson/KVa2d/16/
var Tooltip
Tooltip = function(tip) {
this.tip = tip;
this.buildDOM();
};
$.extend(Tooltip.prototype, google.maps.OverlayView.prototype, {
buildDOM: function() {
this.wdiv = $("<div></div>").addClass('Window').append(this.tip);
this.close();
},
onAdd: function() {
$(this.getPanes().floatPane).append(this.wdiv);
},
onRemove: function() {
this.wdiv.detach();
},
draw: function() {
var pos, left, top;
if (!this.getProjection()) return;
if (!this.get('position')) return;
pos = this.getProjection().fromLatLngToDivPixel(this.get('position'));
top = pos.y - this.getAnchorHeight() / 2 - this.wdiv.outerHeight()/2;
if (this.getMap().getCenter().lng() > this.get('position').lng()) {
left = pos.x + this.wdiv.outerWidth() * 0.3;
} else {
left = pos.x - this.wdiv.outerWidth() * 1.3;
}
this.wdiv.css('top', top);
this.wdiv.css('left', left);
},
open: function(map, anchor) {
if (map) this.setMap(map);
if (anchor) {
this.set('anchor', anchor);
this.bindTo('anchorPoint', anchor);
this.bindTo('position', anchor);
}
this.draw();
this.wdiv.show();
this.isOpen = true;
},
close: function() {
this.wdiv.hide();
this.isOpen = false;
},
getAnchorHeight: function() {
return -1 * this.get('anchorPoint').y;
}
});
资源
Tooltip
代码包含在其中。Tooltip
不是API的一部分,并且没有表明它的实现方式。 - Andrew Leach