我可以像这样添加标签给圆圈标记
L.circleMarker(points[i],{title: 'unselected'}).bindLabel('Destination').addTo(map);
这会添加鼠标悬停在圆圈标记上的标签。
但我想添加静态标签,无论鼠标是否在该圆圈标记上都会出现。
我指的是此演示版http://leaflet.github.com/Leaflet.label/,用于向圆圈标记添加静态标签,但有些我无法做到这一点。 它可以正常使用标记,但是使用圆圈标记静态标签不起作用。
还有其他方法可以在圆圈标记上添加标签吗?
答案 0 :(得分:11)
L.CircleMarker
从L.Path
而不是L.Marker
延伸,因此如果您比较https://github.com/Leaflet/Leaflet.label/blob/master/src/Path.Label.js和https://github.com/Leaflet/Leaflet.label/blob/master/src/Marker.Label.js,您会发现Path
没有L.CircleMarker.include({
bindLabel: function (content, options) {
if (!this._label || this._label.options !== options) {
this._label = new L.Label(options, this);
}
this._label.setContent(content);
this._labelNoHide = options && options.noHide;
if (!this._showLabelAdded) {
if (this._labelNoHide) {
this
.on('remove', this.hideLabel, this)
.on('move', this._moveLabel, this);
this._showLabel({latlng: this.getLatLng()});
} else {
this
.on('mouseover', this._showLabel, this)
.on('mousemove', this._moveLabel, this)
.on('mouseout remove', this._hideLabel, this);
if (L.Browser.touch) {
this.on('click', this._showLabel, this);
}
}
this._showLabelAdded = true;
}
return this;
},
unbindLabel: function () {
if (this._label) {
this._hideLabel();
this._label = null;
this._showLabelAdded = false;
if (this._labelNoHide) {
this
.off('remove', this._hideLabel, this)
.off('move', this._moveLabel, this);
} else {
this
.off('mouseover', this._showLabel, this)
.off('mousemove', this._moveLabel, this)
.off('mouseout remove', this._hideLabel, this);
}
}
return this;
}
});
L.circleMarker([53.902257, 27.541640] ,{title: 'unselected'}).addTo(map).bindLabel('Destination', { noHide: true });
任何选项和这个逻辑你必须自己实现。例如:
{{1}}
答案 1 :(得分:1)
只是想为tbicr的响应添加更新或更正(不确定API是否在响应后更新)。
你可以这样做:
// First add your GeoJSON layer
geojson = L.geoJson(myGeoJson,{
onEachFeature: onEachFeature
}).addTo(map);
// onEachFeature is called every time a polygon is added
var polys = [];
function onEachFeature(layer){
polys.push(layer); // Push the polygons into an array you can call later
}
// Now trigger them after they've been added
$('a').click(function(){
polys[0].fire('click') // clicks on the first polygon
polys[1].fire('click') // clicks on the second polygon
});