leaflet - Unable to display correct coordinates of a selected marker -
i built map leaflet (inside qlikview extension) many markers. want users go map , when double click on marker, displays coordinates. following code, when click on marker, have coordinates of last marker fetched, not selected one. want put coordinates in latsel e lonsel variables.
for (var i=0,k=_this.data.rows.length;i<k;i++){ var row = _this.data.rows [i]; var latitude = parsefloat(row[0].text.replace(",",".")); var longitude = parsefloat(row[1].text.replace(",",".")); //check see coordinates valid if (latitude != nan && latitude !='' && latitude <= 90 && latitude >= -90 && longitude != nan && longitude !='' && longitude <= 180 && latitude >= -180) { var latlng = new l.latlng(latitude, longitude); var poptext = 'lat & long:'+latlng+'<br/>'+ row[2].text +'<br/>'+ 'measure: ' + row[4].text; var marker = l.marker(latlng).addto(map).bindpopup(poptext); marker.on('dblclick', function(e){ latsel = marker.getlatlng().lat; lonsel = marker.getlatlng().lng //_this.data.selecttextsincolumn(0, true, latsel); alert("latitude = " + latsel); }); } else { // } }
what i'm doing wrong? in advance.
assigning onclick
listeners in loops can bit tricky (search "javascript onclick loop" in favourite search engine , find solutions).
instead of using marker
variable, in leaflet can use variable this
inside callback refers marker, so:
marker.on('dblclick', function(e){ latsel = this.getlatlng().lat; lonsel = this.getlatlng().lng; alert(latsel); });
see demo code here: http://plnkr.co/edit/ru3ssoi6a16ayoqaq0xv?p=preview
Comments
Post a Comment