Dwain
Dwain

Reputation: 53

Mapbox: Make line cross the meridian

I have some trouble with the mapbox editor and the api itself.

I have 4 coordinates and cross the meridian first time from west to east and afterwards from east to west. Unfortunately mapbox does not seem to be able to provide a route (via directions api), because the lng values are not in a valid range (-180 to 180)

https://docs.mapbox.com/mapbox-gl-js/example/line-across-180th-meridian/

The Mapbox documentation provides that code sample, but that code sample generates invalid lng values, e.g. 200.

Response from direction api:

code: "InvalidInput"
message: "Longitude must be between -180 and 180"

Do you have an idea how to fix that?

My current function, that tries to update the lng values according to the mapbox documentation:

function prepareForAntimeridian(coords: Array<Array<number>>) {
  return coords.map(([endLng, endLat], idx) => {
    if (idx === 0) {
      return [endLng, endLat]
    }
    const [startLng] = coords[idx - 1]

    if ((startLng < 0 && endLng > 0) || (startLng > 0 && endLng < 0)) {

      if (endLng - startLng >= 180) {
        return [endLng - 360, endLat]
      } else if (endLng - startLng < 180) {
        return [endLng + 360, endLat]
      }
    } else {
      return [endLng, endLat]
    }
  })
}

Here a set of coordinates:

[[151.21, -33.868], [174.78333, -36.85], [-159.78311986642655, -21.23477668120387], [176.315163653344, -6.28729607840079], [171.489195, 7.124354], [165.715489, -21.316338], [151.21, -33.868]]

As you can see, the lines between point 2 and 3 and 3 and 4 cross the meridian.

Do you have any ideas?

Thanks in advance !!!

Upvotes: 0

Views: 34

Answers (0)

Related Questions