Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use the .off() event method in leaflet.js?

Tags:

I am trying to build a map application using leaflet.js and I can not figure out how to use the .off method. The documentation doesn't have any examples and I can't seem to find anything anywhere else online. I have distilled the problem into a more simple chunk of code so my question could be more clear.

Basically I have it set up so that when you click the 'enable click' link it will add an event listener that adds a marker to the map every time you click it. I want to remove that event listener when you click 'disable click'.

Here is a link to the demo

Here is the code I have now.

$(document).ready(function(){

var map, cloudmade, sanAntonio, polygonPoints  


 map = new L.Map('map');
 cloudmade = new L.TileLayer('http://{s}.tile.cloudmade.com/d4334cd6077140e3b92ccfae2b363070/997/256/{z}/{x}/{y}.png', {
    attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://cloudmade.com">CloudMade</a>',
    maxZoom: 18
});


 sanAntonio = new L.LatLng(29.4238889, -98.4933333); // geographical point (longitude and latitude)


 map.setView(sanAntonio, 13).addLayer(cloudmade);

//everything above sets up the map



function enableClick(){
  map.on('click', function(e) {    
    var marker = new L.Marker(e.latlng, {draggable:true});
    map.addLayer(marker);
  });//closes the click function

  this.disableClick = function(){
    map.off('click');
  }

}



//when 
$('#enable_click').click(function(){
  var enable_click = new enableClick()

  $('#disable_click').click(function(){
    enable_click.disableClick;
  });

});




});//closes the document ready function

I have already tried a bunch of different things so the whole 'this.disableClick' thing is just the latest strange thing I have tried. Anyone have a clue?

like image 498
Spencer Cooley Avatar asked Apr 25 '12 22:04

Spencer Cooley


People also ask

How do you remove attribution in Leaflet?

If you want to just remove the Leaflet attribution from the control, while maintaining individual layer attribution, you can use the setPrefix method as documented here: http://leafletjs.com/reference.html#control-attribution.

How do I add a click event to a marker Leaflet?

Follow the steps given below to add events to the map. Step 1 − Create a Map object by passing a <div> element (String or object) and map options (optional). Step 2 − Create a Layer object by passing the URL of the desired tile. Step 3 − Add the layer object to the map using the addLayer() method of the Map class.

What is TileLayer in Leaflet?

Used to load and display tile layers on the map, implements ILayer interface.

How do you use a Leaflet in react JS?

We must import Map from react-leaflet (along with some other packages that we'll utilize later), and we'll return it from our App component: import React, {useState} from "react"; import { MapContainer, TileLayer, Marker, Popup } from 'react-leaflet'; import './App.


1 Answers

You need to pass the function to on and off by reference:

function doStuff() { ... }

map.on('click', doStuff);
...
map.off('click', doStuff);
like image 151
Mourner Avatar answered Sep 19 '22 11:09

Mourner