Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change img.src and map on the fly -> Error Unable to set property 'coords' #66

Open
SarahTrees opened this issue May 31, 2018 · 6 comments

Comments

@SarahTrees
Copy link

Error: Unable to set property 'coords' of undefined or null reference

Everything works the way we want it first time, excellent, thank you for the tool!

Our users can change the src of the image. After that we delete all area from the map and put new area in the map.

There are problems here. The tool saves the old areas between and wants to adjust them after the src change. But at this time the map has no area.

Error: Unable to set property 'coords' of undefined or null reference
Line 20: areas[idx].coords = cachedAreaCoords.split(',').map(scale).join(',');
Reason: areas[idx] is undefined.

How can we change the src and build the new map without having this error?

The tool was initialized by $ ('#mainpicture map').ImageMapResize();
How can we de-initialize / destroy / delete / remove it? To reinitialize it after setting all the HTML details?

@davidjbradshaw
Copy link
Owner

Can you try the version in this PR and let me know if it works for you?

#57

@nzlrhyz
Copy link

nzlrhyz commented Sep 23, 2019

@davidjbradshaw I also have a pretty identical use case to this I have tried the linked PR however it still comes up Cannot set property 'coords' of undefined

@nzlrhyz
Copy link

nzlrhyz commented Oct 17, 2019

I fixed this issue for myself the two changes that I believe are needed are

if (true==true) {
            setup()
            addEventListeners()
            start()
        } else {
            map._resize() //Already setup, so just resize map
        }

Which just negates the beenHere check

and

function resizeAreaTag(cachedAreaCoords, idx) {
                function scale(coord) {
                    var dimension = 1 === (isWidth = 1 - isWidth) ? 'width' : 'height'
                    return (
                        padding[dimension] +
                        Math.floor(Number(coord) * scalingFactor[dimension])
                    )
                }

                if (areas.length === 0) {
                    setup()
                    addEventListeners()
                    start()
                }else{
                    var isWidth = 0

                    areas[idx].coords = cachedAreaCoords
                        .split(',')
                        .map(scale)
                        .join(',')
                }
                
            }

Which stopped the error about Coords not being set, It seem to call it too early (resulting in the error) and then call again which worked

@SarahTrees
Copy link
Author

@nzlrhyz: Thanks for the solution! We had given up in 2018 and today I was able to realise our requirement with your trick.

@davidjbradshaw
It would be nice if there would be a function destroy() where everything would be set to start, so everything would be implemented cleanly.
... and thanks for the script it is very helpful!

@davidjbradshaw
Copy link
Owner

Happy to accept a PR

@Baraw
Copy link

Baraw commented Dec 2, 2023

I fixed this issue for myself the two changes that I believe are needed are

if (true==true) {
            setup()
            addEventListeners()
            start()
        } else {
            map._resize() //Already setup, so just resize map
        }

Which just negates the beenHere check

and

function resizeAreaTag(cachedAreaCoords, idx) {
                function scale(coord) {
                    var dimension = 1 === (isWidth = 1 - isWidth) ? 'width' : 'height'
                    return (
                        padding[dimension] +
                        Math.floor(Number(coord) * scalingFactor[dimension])
                    )
                }

                if (areas.length === 0) {
                    setup()
                    addEventListeners()
                    start()
                }else{
                    var isWidth = 0

                    areas[idx].coords = cachedAreaCoords
                        .split(',')
                        .map(scale)
                        .join(',')
                }
                
            }

Which stopped the error about Coords not being set, It seem to call it too early (resulting in the error) and then call again which worked

THANK YOU VERY MUCH!!

I had the same issue about changing the source image + changing its map dynamically. It is now working !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants