2013-03-21 9 views
7

jestem w stanie dodać etykietę do circlemarker jak tenetykieta dla markera okręgu w ulotce

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

to dodaje etykietę, która pojawia się na najechaniu myszką na okręgu markera.

Ale chcę dodać statyczną etykietę, która pojawi się bez względu na to, czy mysz znajduje się na tym znaczniku koła, czy nie.

Mam na myśli to demo http://leaflet.github.com/Leaflet.label/ za dodanie statycznej etykiety do znacznika okręgu, ale niektóre jak nie jestem w stanie to zrobić. Działa dobrze z markerów, ale z kółkiem Marker statyczny nie działa.

Czy jest jeszcze jakaś inna metoda dodawania etykiety do znacznika koła?

Odpowiedz

11

L.CircleMarker przedłużony z L.Path nie L.Marker, więc jeśli porównać https://github.com/Leaflet/Leaflet.label/blob/master/src/Path.Label.js i https://github.com/Leaflet/Leaflet.label/blob/master/src/Marker.Label.js może się okazać, że Path nie posiada żadnych opcji i ta logika musi realizować siebie. Na przykład:

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

przeniosłem linię 'this._showLabel ({poĹ,oĹĽenia geograficznego: this.getLatLng()});' out of the noHide warunkowy do innego dołączonego metody: ' showLabel: function() {...} '. To pozwala mi utworzyć krąg, dodać go do mapy, a następnie wywołać 'circle.showLabel()'. – jakeorr

+0

świetnie! Chociaż nie zniknie, gdy nad etykietami pojawi się wyskakujące okienko ... – Egidi

1

Chciałem tylko dodać aktualizacji lub korekty do wielkiego odpowiedzi tbicr za (nie wiem, czy API aktualizowane po jego odpowiedzi).

Można to zrobić tak:

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