I am working on an application that renders land parcels from PostGIS as polygons in Leaflet. I have an event handler set for 'viewreset' on the map:

this.leaflet_map.on('viewreset', this.viewport.regenerate, this.viewport);

The this in this case is an object I've created, called pt_map. viewport is an object I've created to wrap the methods for regenerating the view based on the current map bounds. My code works on initial load, as well as on panning the map. However, when I zoom in or out, I get the following error in line 4842 of leaflet-src.js:

Uncaught TypeError: Cannot call method 'latLngToLayerPoint' of null

Any ideas? My full code is available at https://github.com/CoherentLogic/ptarmigan/blob/experimental/web/ptarmigan/parcels/layers.js


try wrapping the function you call in a closure

this.leaflet_map.on('viewreset', function(e){this.viewport.regenerate(e)}, this);

it's vastly more complex than what I'm about to say but as a general rule of thumb it's a bad idea to use a function with a dot in it as an argument to something else without using a closure.

  • 1
    check the scope of this inside of the anonymous function. It may not mean what you think it means. – raykendo May 23 '13 at 13:46
  • my bad especially as my answer was telling him not to do that, fixed the typo. – Calvin May 25 '13 at 0:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.