2013-03-21 64 views
7

環形標誌,我可以添加標籤circlemarker這樣標籤在單張

L.circleMarker(points[i],{title: 'unselected'}).bindLabel('Destination').addTo(map); 

這增加了其鼠標懸停時出現在圓圈標記標籤。

但我想添加靜態標籤,無論鼠標是否在該圓形標記上,都會出現。

我指的是這個演示http://leaflet.github.com/Leaflet.label/爲圓標記添加靜態標籤,但一些我不能做到這一點。 它與標記正常工作,但與圓標記靜態標籤不起作用。

也有任何其他方法來添加標籤上的圓形標記?

回答

11

L.CircleMarkerL.PathL.Marker擴展,所以如果你比較https://github.com/Leaflet/Leaflet.label/blob/master/src/Path.Label.jshttps://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

我移動線'this._showLabel({經緯度:this.getLatLng()});'出noHide條件的成另一種方法包括:' showLabel:function(){...}'。這允許我創建圓,將其添加到地圖,然後調用'circle.showLabel()'。 – jakeorr 2014-05-30 18:29:29

+0

太棒了!雖然在標籤上方顯示彈出窗口時它不會消失... – Egidi 2015-02-09 09:10:58

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 
});