Sunday 15 April 2012

javascript - Opening a leaflet popup on a layerGroup -



javascript - Opening a leaflet popup on a layerGroup -

i trying draw country shapes on leaflet map using l.geojson(data).addto(map). want bind popup click event of country shape...

new l.geojson(data, { oneachfeature: function(feature, layer) { layer['on']('click', popupfunction); } }).addto(this.map); popupfunction = function(event) { var layer = event.target; // open 'add' popup , our content node var bound = layer.bindpopup( "<div>hello world!</div>" ).openpopup(); // ugly hack html content node popup // because need things var contentnode = $(bound['_popup']['_contentnode']); }

now works fine when info single polygon, because layer attribute passed oneachfeature function that: layer.

however if data multipolygon (i.e. us) stops working because "layer" layergroup (it has _layers) attribute , hence has no _popup attribute , can't _contentnode popup.

it seems should quite mutual thing, wanting popup on layergroup. why have no _popup attribute?

you cannot bind l.popup else l.layer because popup coordinates anchor on.

for l.marker position (l.latlng), l.polygon center (look @ the code see how calculated).

as other cases (like yours), can open popup have decide popup opens:

var popup = l.popup() .setlatlng(latlng) .setcontent('<p>hello world!<br />this nice popup.</p>') .openon(map);

javascript leaflet

No comments:

Post a Comment