Leaflet.draw icon indicating copy to clipboard operation
Leaflet.draw copied to clipboard

Rectangle display bug when drawing in 1.0.3 or 1.0.4 - "type is not defined"

Open joelhickok opened this issue 3 years ago • 17 comments

  • [X ] I'm reporting a bug, not asking for help

How to reproduce

  • Leaflet version I'm using: 1.7.1
  • Leaflet Draw version I'm using: 1.0.4 or 1.0.3
  • Browser (with version) I'm using: Bug is browser independent
  • OS/Platform (with version) I'm using: Max OS 11.5.2

When drawing a rectangle using Leaflet.draw version 1.0.3 or greater, I am seeing the error below. If I rollback to version 1.0.2 the error is gone. Essentially, in 1.0.3 or greater, the rectangle path DOES NOT draw when sizing the rectangle over the map, but the layer is created on the "created" event. In version 1.0.2 the rectangle DOES draw and resize as you drag the rectangle over the map.

leaflet.draw.js:9 Uncaught ReferenceError: type is not defined
    at Object.readableArea (leaflet.draw.js:9)
    at NewClass._getTooltipText (leaflet.draw.js:8)
    at NewClass._onMouseMove (leaflet.draw.js:8)
    at NewClass.fire (Events.js:190)
    at NewClass._touchEvent (leaflet.draw.js:9)
    at NewClass._onTouchMove (leaflet.draw.js:9)
    at handler (DomEvent.js:92)
    at _handlePointer (DomEvent.Pointer.js:98)
    at HTMLDivElement.onMove [as _leaflet_touchmovetouchmove28_26] (DomEvent.Pointer.js:108)

joelhickok avatar Aug 21 '21 17:08 joelhickok

Uncaught ReferenceError: type is not defined at Object.readableArea (leaflet.draw.js:9) at NewClass._getTooltipText (leaflet.draw.js:8) at NewClass._onMouseMove (leaflet.draw.js:8) at NewClass.fire (leaflet-src.js:588) at NewClass._touchEvent (leaflet.draw.js:9) at NewClass._onTouchMove (leaflet.draw.js:9) at handler (leaflet-src.js:2669) at _handlePointer (leaflet-src.js:2102) at HTMLDivElement.onMove [as _leaflet_touchmovetouchmove28_26] (leaflet-src.js:2112) at ZoneDelegate.invokeTask (zone.js:406)

i am also facing the same error while drawing the rectangle

SudhagarMurugesan avatar Nov 23 '21 12:11 SudhagarMurugesan

image

Zeng-Wenquan avatar Nov 26 '21 09:11 Zeng-Wenquan

Since project looks dead, and some people might be wondering how to fix this issue, I want to explain the fix. Sorry for the necroposting and information that might be obvious.

Right after you import Leaflet.Draw, create a global variable named type like so:

require("leaflet-draw");
window.type = true; // Doesn't matter what value to put here, just initialize the thing

Before you do that, verify that none of your dependencies uses this variable! To do so, run your app, open console and type window.type. If it outputs something different from undefined, find and get rid of the library that uses this variable or switch to another drawing library (for example, geoman).

This trick works for my build process, though, you might want to modify it for your build system. For example, it might remove unused object properties, so you have to read or write this value somewhere or add it to the ignore list. How to solve it is up to you.

The whole thing might introduce a drawback. I have no idea how and when readableArea() is called, so I assume there might be some kind of race conditions, so different Leaflet.Draw instances that use different measurements will override this variable in the middle of the execution.

The solution is to be consistent with measurements across your application. Or don't use isMetric parameter and, if you want, disable measurements output.

matafokka avatar Dec 06 '21 11:12 matafokka

This is obviously not feasible and will pollute the global variables, The only way to fix this problem is to create a new branch

zwq000 avatar Dec 20 '21 03:12 zwq000

这是来自QQ邮箱的假期自动回复邮件。你好,我最近正在休假中,无法亲自回复你的邮件。我将在假期结束后,尽快给你回复。

Zeng-Wenquan avatar Dec 20 '21 03:12 Zeng-Wenquan

Yes, I hate creating unnecessary global variables too. Another fix might be to monkey-patch this method. I'm talking about users fixing it, not devs, haha :)

Indeed, it would be great, if problem was fixed by devs, but this project seems abandoned. Is there a patched fork, by any chance? I couldn't find one.

matafokka avatar Dec 20 '21 08:12 matafokka

Any update on this issue. @matafokka solution works. Is there a another cleaner way? How to disable measurements output on draw?

Rafnuss avatar Jan 01 '23 17:01 Rafnuss

这是来自QQ邮箱的假期自动回复邮件。你好,我最近正在休假中,无法亲自回复你的邮件。我将在假期结束后,尽快给你回复。

Zeng-Wenquan avatar Jan 01 '23 17:01 Zeng-Wenquan

I think the fix is just to add type to the var declaration at the top of readableArea. That would be scoped to the function and not conflict with the global namespace.

If you don't want to mess with vendor source code, you can use @matafokka's solution. I just did window.type = '' so that anything that checks if (window.type) still returns false.

jcgiuffrida avatar Jun 21 '23 17:06 jcgiuffrida

这是来自QQ邮箱的假期自动回复邮件。你好,我最近正在休假中,无法亲自回复你的邮件。我将在假期结束后,尽快给你回复。

Zeng-Wenquan avatar Jun 21 '23 17:06 Zeng-Wenquan