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

offsetFn with zoom

Open jjimenezshaw opened this issue 3 years ago • 4 comments

In response to https://github.com/jieter/Leaflet.Sync/issues/59 and https://github.com/jieter/Leaflet.Sync/issues/41 (thanks for the code suggestions; many are included here), this PR lets the user to return an object like {center, zoom} in the callback offsetFn.

To not break backwards compatibility, there is a wrapper function to add the zoom if only the center is returned. So both things can be returned in offsetFn: just the center, or an object with center and zoom.

This is WIP. Still some tests and documentation to be added.

There is one example, examples/multiple_offset_zoom.html, in case you want to have a look.

There is a problem detected. In case of a.sync(b) and b.sync(a) where the zoom changes are not properly correlated, it enters in an infinite loop. Leaflet detects a zoom change, and fires an update in the other.

jjimenezshaw avatar Feb 25 '21 23:02 jjimenezshaw

@jieter @nmoreaud what do you think?

jjimenezshaw avatar Mar 21 '21 15:03 jjimenezshaw

@jieter I had implemented almost all the suggestions. Any more comments?

jjimenezshaw avatar Apr 23 '21 11:04 jjimenezshaw

@jieter what about this PR?

jjimenezshaw avatar Oct 09 '21 07:10 jjimenezshaw

I tried running your fork, however I get the following error

leaflet-src.js:1764 Uncaught TypeError: Cannot read properties of null (reading 'lat')
    at Object.project (leaflet-src.js:1764:1)
    at Object.latLngToPoint (leaflet-src.js:1601:1)
    at NewClass.project (leaflet-src.js:4095:1)
    at NewClass._getNewPixelOrigin (leaflet-src.js:4618:1)
    at NewClass._move (leaflet-src.js:4337:1)
    at NewClass._resetView (leaflet-src.js:4299:1)
    at NewClass.setView (leaflet-src.js:3319:1)
    at NewClass.sync (L.Map.Sync.js:60:1)
    at ./src/app.ts (app.ts:31:9)
    at __webpack_require__ (bootstrap:22:1)

This is the code I use for syncing

mainMap.sync(minimap, {
    offsetFn: function (center: any, zoom: number, refMap: any, targetMap: any) {
        return {center: center, zoom: zoom - 1};
    }
});

MasterPuffin avatar Feb 07 '24 13:02 MasterPuffin