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

Colby Fayock
InstructorColby Fayock
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years 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.

egghead
egghead
~ 18 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today