我最近在寻找同样的问题,昨天刚刚根据谷歌组中的帖子实现了它。
https://groups.google.com/forum/#!topic/leaflet-js/sA2HnU5W9Fw
感谢 Adrian 提供的原始代码示例。
这是解决方案:
扩展如下类:
<script>
L.LabelOverlay = L.Class.extend({
initialize: function(/*LatLng*/ latLng, /*String*/ label, options) {
this._latlng = latLng;
this._label = label;
L.Util.setOptions(this, options);
},
options: {
offset: new L.Point(0, 2)
},
onAdd: function(map) {
this._map = map;
if (!this._container) {
this._initLayout();
}
map.getPanes().overlayPane.appendChild(this._container);
this._container.innerHTML = this._label;
map.on('viewreset', this._reset, this);
this._reset();
},
onRemove: function(map) {
map.getPanes().overlayPane.removeChild(this._container);
map.off('viewreset', this._reset, this);
},
_reset: function() {
var pos = this._map.latLngToLayerPoint(this._latlng);
var op = new L.Point(pos.x + this.options.offset.x, pos.y - this.options.offset.y);
L.DomUtil.setPosition(this._container, op);
},
_initLayout: function() {
this._container = L.DomUtil.create('div', 'leaflet-label-overlay');
}
});
</script>
此外添加这个CSS:
<style>
.leaflet-popup-close-button {
display:none;
}
.leaflet-label-overlay {
line-height:0px;
margin-top: 9px;
position:absolute;
}
</style>
然后显示如下文本标签:
<script>
var map = L.map('map').setView([51.898712, 6.7307100000001], 4);
// add markers
// ...
// add text labels:
var labelLocation = new L.LatLng(51.329219337279405, 10.454119349999928);
var labelTitle = new L.LabelOverlay(labelLocation, '<b>GERMANY</b>');
map.addLayer(labelTitle);
var labelLocation2 = new L.LatLng(47.71329162782909, 13.34573480000006);
var labelTitle2 = new L.LabelOverlay(labelLocation2, '<b>AUSTRIA</b>');
map.addLayer(labelTitle2);
// In order to prevent the text labels to "jump" when zooming in and out,
// in Google Chrome, I added this event handler:
map.on('movestart', function () {
map.removeLayer(labelTitle);
map.removeLayer(labelTitle2);
});
map.on('moveend', function () {
map.addLayer(labelTitle);
map.addLayer(labelTitle2);
});
</script>
结果: