Fix React Leaflet Polyline International Date Line Wrapping with the Antimeridian Plugin

Colby Fayock
InstructorColby Fayock

Share this video with your friends

Send Tweet
Published 2 years ago
Updated a year ago

In this lesson, we'll use the Leaflet Antimeridian plugin to fix issues with Leaflet Polylines when crossing the International Date Line. We'll walk through installing the plugin using it's Github source, importing its fix as a WrappedPolyline function, and using it to naturally draw our Leaflet line data on our map.

Leaflet.Antimeridian

Colby Fayock: [0:00] We're going to start off with a basic map for Santa's route to deliver presents. If you notice on the edge, we start to get this weird effect where the lines zigzag across the map. The issue is by default, Leaflet can't understand the coordinates that go across the International Date Line.

[0:12] To fix this, we're going to use a Leaflet plugin called Antimeridian. It allows these lines to draw naturally on the map. This plugin isn't available on npm, so we're going to install it using its GitHub address. Once that's installed, we can start back up our server.

[0:25] Inside our code, we're importing a Map and a TileLayer from 'react-leaflet'. We're using that Map and TileLayer component to create our app. We're also importing the Polyline from 'leaflet', which allows us to import santaStops, create a new Polyline instance, and add it to our map.

[0:38] To get started with the Antimeridian, we're going to import the Polyline from the plugin source. Since we already have a Polyline, to avoid conflicts, we're going to import this one as WrappedPolyline. With that imported, we're going to simply replace our Polyline with a WrappedPolyline. Once I reload the page, we can see instead of zigzagging, the route draws naturally on each end.

[0:58] In review, we installed the Leaflet Antimeridian plugin. Since it's not available on npm, we had to install it from its GitHub. With that installed, we imported the Polyline as a WrappedPolyline from the Antimeridian source. With that installed, we were able to simply replace our Polyline with a WrappedPolyline. With our plugin installed, we can see our route draw out naturally on a map.