Leaflet.MapCenterCoord
Leaflet.MapCenterCoord copied to clipboard
Coords change when center-zooming
I've noticed that coords change while zooming. Note I'm using options that should ensure no such changes...
doubleClickZoom: 'center',
scrollWheelZoom: 'center',
touchZoom: 'center',
To reproduce, simply note the coords, zoom in/out, and note the coords again. Note you can't use the demo website to test, as it doesn't use the aforementioned options.
I found that if I programmatically go to a location while zoomed in, the bug doesn't manifest. For example...
map.setView(coords, 18);
Now I can zoom in/out and the coords don't change.
Admittedly, the bug may be with leaflet itself. I'm using 1.5.1.
I tried this too with leaflet 0.75 and 1.5.1. I see something similar and it seems to have something to do with negative positions.
Using the example with 39.588, 2.935 (Palma) I see no problem with zooming in/out. If I change this to -39.588 (in the Atlantic between Africa and South America) and then zoom I some back to Palma.
This solves my problem: Explanation: On lines 94 and 98 of L.Control.MapCenterCoord.js the author makes the lat/lng positions absolute. The format function on line 134 also makes the positions absolute so I presumed it expects the positions as is using +/- formats but it never gets to see a negative. Solution for me: Comment out lines 94 and 98 and use a local copy of L.Control.MapCenterCoord.js
Caveat: Have not analysed the >180 degrees logic at line 100 but presume it to be correct.
Tested with Win10 Firefox 69.0.1 Edge44.18362 with leaflet 0.75 and 1.5.1
Awesome! I will test this as well.
You can help the author by forking the project, making the changes, and asking for a pull request.
OK your edit significantly helps but doesn't totally resolve the issue. If you move the map with your mouse or finger, note the coords, then zoom, you will still see the coords change.
Also, is there a reason you're using the leaflet1 branch instead of master? I'm using master. My understanding of github is that branches are ephemeral and should really be pulled into master once they're complete.
I see the coords change but just by slight seconds. like (14.9 to 13.2)
I have no idea what "leaflet1 branch instead of master" means.I downloaded Leaflet.MapCenterCoord-0.7.zip
On Tuesday, October 1, 2019, 8:55:42 p.m. EDT, Exscotticus <[email protected]> wrote:
OK your edit significantly helps but doesn't totally resolve the issue. If you move the map with your mouse or finger, note the coords, then zoom, you will still see the coords change.
Also, is there a reason you're using the leaflet1 branch instead of master? I'm using master. My understanding of github is that branches are ephemeral and should really be pulled into master once they're complete.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.
"You can help the author by forking the project, making the changes, and asking for a pull request."I have no idea what this means...
On Tuesday, October 1, 2019, 8:37:38 p.m. EDT, Exscotticus <[email protected]> wrote:
Awesome! I will test this as well.
You can help the author by forking the project, making the changes, and asking for a pull request.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.
OK, I have validated the problem you mentioned. I put a coord point in Montreal using zoom 18 then zoomed to about 5 and back to 18. No change. I moved the map slightly and did the same test. I am about 250-280 meters (800-900 feet) away. Hmmm.
My guess is the problem is not in Leaflet.MapCenterCoord itself but in Leaflet.
MapCenterCoord makes calls to Leaflet's latLngToLayerPoint and I suspect the problem is there. I checked the code and there is a pixel point(s) and rounding going on. Have no idea how many decimals in the calculations but it would not take many iterations to slightly skew the coords.
Yes isn't this strange? I mean let's say it's a rounding bug in leaflet. How is it that it only manifests when you move the map slightly? And not when you programmatically go to a set of coords? You would think the rounding bug would apply either way.
Here are my deductions. It has to do with coord precision, zoom factor, pixels, and rounding. I tested at max zoom 18. The decimal portion in MapCenterCoord shows as 5 digits when using format DD. I entered decimal portions lat/log as 4883051/6036051 which got rounded. I also placed a standard blue marker there too. I open my map in a tab. Now the system computes what pixel (x,y) in the map bounds which corresponds to that coord to place it markers. Both are dead on. I move the map anywhere then move it back to my original spot using just the DD coord numbers shown by MapCenterCoord. I open my map in another tab and take snapshots of both. Flipping between tabs it is obvious that they don't line up. I look at my snaphots and, in the map I moved, I am off by 2 pixels, 1 right and 1 down. Zoom out 10 times and then back in and it gets magnified. So how many pixels would correspond to the coord entered in MapCenterCoord? Changing DD to show 7 digits makes it really hard to move back to the original spot and I could not do it so I gave up trying.
It seems I was wrong about the "> 180 degrees logic" at line 100 and presuming it was correct. It is flawed also. Because I commented out the Math.abs() function in lines 94 and 98, line 101 needs to be changed to center.lng = center.lng - 360; in order for the coord to be negative.
I finally understood about the "leaflet1 branch instead of master" and the confusion. This issue bothered me because I'm getting the same results with my "old" copy (but with my 3 changes) as I get with the master. My copy pre-dates the last 2 commits on Jul 28, 2016 and Aug 28, 2016.
I got it from xguaita.github.io clicked "get latest" and picked src.
..... and I threw in your changes for the zoom-zoom.
My comments above were all based on this "old" copy which is why there was confusion. My first time on github and not used to how it works.
I was happy to see your discussions because this bug is a show stopper. If I understand things correctly, the author is manipulating the center coord object in leaflet with the absolute value. Well, this will completely mess up basemap layers. For a general example, in my case, say I'm viewing a U.S. location using a U.S. basemap. I zoom and then, wham, I'm sourcing blank tile errors because my location has suddenly changed to Asia.
Your changes are sweet. However, the displayed coordinates in the legend also need to be modified. I changed lines 124 and 125 as follows:
x: (!this.options.latlngDesignators ? lng : (center.lng_neg ? lng.substr(1) : lng)) + (this.options.latlngDesignators ? (center.lng_neg ? ' W' : ' E') : ''), y: (!this.options.latlngDesignators ? lat : (center.lat_neg ? lat.substr(1) : lat)) + (this.options.latlngDesignators ? (center.lat_neg ? ' S' : ' N') : '')
This works for 'DD' format, haven't looked at DM or DMS. This stuff really isn't my expertise so if you see something wrong please call me out on it.
Thanks.
I can't see your issue because I used the version from the author's original page which was v0.7 released Jan 25, 2016 and not the master from Aug. 28 2016. This was an accident because I had no idea how Github worked. You can see the confusion in some of my comments.
Naively I plodded along to figure this out and I realised that the commit on Jul 28, 2016 was an attempt to fix the problem you described: "I'm viewing a U.S. location using a U.S. basemap. I zoom and then, wham, I'm sourcing blank tile errors because my location has suddenly changed to Asia." This was my problem too. I was using Montreal as my base.
The commit on Jul 28, 2016 was a band-aid to fix the symptom of the problem but not the cause.
The solution I posted, on October 1, fixed the problem by commenting out 2 lines of Math.abs() and this is what I am using. https://github.com/xguaita/Leaflet.MapCenterCoord/issues/14#issuecomment-537253099 Then, on October 6, I posted another fix related to my October 1 post. https://github.com/xguaita/Leaflet.MapCenterCoord/issues/14#issuecomment-538778592
Remember all my line numbers are based on v0.7 and not the master.
Your changes are relative to the master from Github and not the author's original page v0.7.
You can try my way to see if it is good for you. I consider my solution to be more elegant by removing redundant code but this is my opinion and I am biased.
I have no idea how to do commits, pulls, branches, and forks so I left it there. I'm sure it works but this Github thing looks confusing.
Well … to be clear, I am indeed using the latest master from Aug. 28 2016 (which doesn't work). I made your 2 sets of changes, exactly like you did and they corresponded exactly to the same line numbers. I then found that I had the additional problem that I described above and then fixed. So my conclusion is that the final github release will not work at all.
I don't know a lot about github, but I download a lot and the released version is expected to work. I have no idea why it doesn't, but with our 3 fixes it's been working fine for me.
Then I will revisit the code to test your changes in what I use. Now does the "fixed" v0.7 play well with other plugins? I may have incorporated other plugins in what I use.
On Saturday, December 7, 2019, 10:14:27 a.m. EST, mtwhitney <[email protected]> wrote:
Well … to be clear, I am indeed using the latest master from Aug. 28 2016 (which doesn't work). I made your 2 sets of changes, exactly like you did and they corresponded exactly to the same line numbers. I then found that I had the additional problem that I described above and then fixed. So my conclusion is that the final github release will not work at all.
I don't know a lot about github, but I download a lot and the released version is expected to work. I have no idea why it doesn't, but with our 3 fixes it's been working fine for me.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.
I'm using two plugins: gpx and fullscreen - both play well with this one. Your question is a good one because the plugin that failed for me (with this one) and drove me here in the 1st place was zoomhome.
The "home" plugins will all fail w/MapCenterCoord because the leaflet center coord is changed with the abs value number. Any subsequent map zoom or movement and then clicking the home button will cause the map to go somewhere that is certainly not home. (The "home" plugins do not object copy the original center coords.)
Your 2 fixes should solve this issue. I have not gone back to use zoomhome yet, but I intend to and I expect it to now work.
I checked your code and from what I can see and test, it returns absolute latitudes. I suspect your US database has all coordinates as absolute and somewhere it specifies North and South or East and West. So I can see that with a negative latitude you would end up in Asia. All North America is negative latitudes. When using the designators NS EW all numbers are written as positive.
The master works because of the 2 commits which made copies of the centers. Remember I started with a wrong version which why I did what I did by just commenting out the Math.abs() lines. With what I did I messed up DM and DMS and I know how to fix it.
I tested zoomhome and MapCenterCoord together in one html file with Montreal coordinates and it works correctly. I zoom, I move, and I hit home and I'm back.
On Saturday, December 7, 2019, 11:15:30 a.m. EST, mtwhitney <[email protected]> wrote:
I'm using two plugins: gpx and fullscreen - both play well with this one. Your question is a good one because the plugin that failed for me (with this one) and drove me here in the 1st place was zoomhome.
The "home" plugins will all fail w/MapCenterCoord because the leaflet center coord is changed with the abs value number. Any subsequent map zoom or movement and then clicking the home button will cause the map to go somewhere that is certainly not home. (The "home" plugins do not object copy the original center coords.)
Your 2 fixes should solve this issue. I have not gone back to use zoomhome yet, but I intend to and I expect it to now work.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.
Great! Good to know when I get back to it.