unable to limit leaflet marker on click event to only go off once

Newbie

So I have leaflet markers for 10 cities in a country created from a api ajax call. When a user clicks on one of the city markers a different ajax call is made that gets 20 nearby place Wikipedia articles and adds a marker for each place on the map. It all works fine however if the city marker is clicked again the ajax call is made again and a duplicate 20 markers are added each time the city marker is clicked. I want to prevent multiple ajax calls/duplicate markers.

I've tried

.one( "click", function() {//my code});

however the result is the following error

L.marker(...).bindPopup(...).one is not a function

I've also tried

$(this).off(event);

any help would be appreciated, thank you.

My js code:

var largeCityMarker = L.marker(new L.LatLng(cityLat, cityLng), ({icon: cityIcon}))
.bindPopup(`<div class="markerContainer"><h3>${cityName}</h3><img class="markerThumbnail" src='${cityThumbnailImg}' onerror="this.style.display='none'"><p class="markerTxtDescription">${cityInfo}</p><div id="city-link"><a href="//${cityUrl}" target="_blank">${cityText}</a></div></div>`, cityOptions)
.on('click', function() {
                        $.ajax({
                            url: "assets/php/wikiLoops.php",
                            type: 'GET',
                            dataType: 'json',
                            data: {
                                lat: this.getLatLng().lat,
                                lng: this.getLatLng().lng,
                                countryCodeA2: borderCountryCode
                            },
                        
                            success: function(result) {
      //wiki Find Nearby Places for cities
                                wikiCluster = new L.markerClusterGroup();
                                console.log(result);
                                result.data.wikiCitiesData.geonames.forEach(place => {
                                    
                                    var wikiPlaceIcon = L.icon({
                                        iconUrl: 'assets/img/icons/wikipedia.png',
                                        iconSize: [50, 50], // size of the icon
                                        popupAnchor: [0,-15]
                                        });
                                    var customOptions =
                                        {
                                        'maxWidth': '300',
                                        'className' : 'custom'
                                        };
                                        
                                    wikiPlaceName = place.title;
                                    wikiPlaceLat = place.lat;
                                    wikiPlaceLng = place.lng;
                                    wikiSummary = place.summary;
                                    wikiUrl = place.wikipediaUrl;
                                    wikiThumbnail = place.thumbnailImg;
                                    
                                    var customPopup = `<div class="card" style="width: 18rem;">
<div class="card-body"><h5 class="card-title">${wikiPlaceName}</h5><img class="img-thumbnail float-right" style="max-width: 100px" src="${wikiThumbnail}" onerror="this.style.display='none'"><p class="card-text" id="wiki-sum">${wikiSummary}</p><a href="//${wikiUrl}" target="_blank"class="card-link">Read more</a><a href="#" class="card-link"></a></div></div>`;
                                    
                                    wikiPlaceMarker = L.marker(new L.LatLng(wikiPlaceLat, wikiPlaceLng), ({icon: wikiPlaceIcon})).bindPopup(customPopup,customOptions);
                                    console.log(wikiPlaceMarker);

                                    capCityCluster.addLayer(wikiPlaceMarker);  
                                    
                                });
                            },
                            error: function(jqXHR, textStatus, errorThrown) {
                                console.log("wikiLoopPHP",textStatus, errorThrown);
                            }
                        });
                     });
    
                    largeCityCluster.addLayer(largeCityMarker);
                    
                    });

                });
                
            }
        
        },
ghybs

Looks like you confuse jQuery syntax with Leaflet syntax.

In Leaflet, attaching an event listener that should fire only once then be removed automatically is made using the once method:

Behaves as on(…), except the listener will only get fired once and then removed.

L.marker(latLng)
  .bindPopup(popupContent)
  .once('click', function() {});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Marker in leaflet, click event

From Dev

Leaflet JS Cluster Marker cancel click event

From Dev

How to disable marker click event?

From Dev

Fire Event to Open CartoDB/Leaflet Marker

From Dev

bind a click event only once for dynamically created elements

From Dev

Zoom and center marker on click in leaflet

From Dev

Click Event Works Only For Last Marker

From Dev

Leaflet popupclose event fires whilst dragging marker

From Dev

How to zoom on marker click event in Mapbox Leaflet?

From Dev

jQuery: why click event works only once?

From Dev

Event .click () on and off

From Dev

How to disable click event on leaflet map only?

From Dev

Panel item listener of click event only fires once

From Dev

Click event handler only working once in Backbone.js

From Dev

Jquery .click event on .fadeToggle with animated text only firing once

From Dev

Limit button click to only once

From Dev

jQuery: why click event works only once?

From Dev

Kendo Tabstrip click event fired only once

From Dev

Event on marker click?

From Dev

Leaflet marker icon click event is not working

From Dev

Leaflet marker event fires at wrong time

From Dev

loading script only once on click event

From Dev

On click event only firing once JQuery / bPopup

From Dev

Click event on android buttons (binding only once)

From Dev

Getting Leaflet marker click event to work with Angular 2

From Dev

jQuery Click Event triggering only once

From Dev

Highcharts click event should apply only on marker point

From Dev

Call the event handler only once even when on double click

From Dev

Zoom on click in marker using leaflet for more then actions

Related Related

HotTag

Archive