question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

setBounds zooms to incorrect location when over antemeridian

See original GitHub issue

If you call setting bounds on the camera with coordinates crossing the antemeridian (eg { ne: [150, 20], sw: [-179, -20] } it zooms to the map as if the bounds had west and east swapped (eg { ne: [-179, 20], sw: [150, -20] }`. Instead it should zoom the map to the area crossing the antemeridian (line at ±180 lon).

To Reproduce use the starter instructions, and the following map code

          <MapboxGL.MapView style={styles.map} >
            <MapboxGL.Camera bounds={{
              ne: [150, 20],
              sw: [-179, -20],
            }}/>
            </MapboxGL.MapView>

this zooms here: Simulator Screen Shot - iPhone 11 - 2020-05-15 at 09 19 27

Expected behavior it should zoom to a region in the pacific eg Simulator Screen Shot - iPhone 11 - 2020-05-15 at 09 20 08

Versions (please complete the following information):

  • Platform: both
  • Device: iPhone X
  • Emulator/ Simulator: yes
  • OS: iOS 13.4.1
  • react-native-mapbox-gl Version [e.g. 7.0.9]
  • React Native Version 0.62.2

This could be a problem inside of mapbox itself, im not too sure how the camera/zooming works

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
jaysquaredcommented, Oct 14, 2020

In MapboxGL it’s implemented with setVisibleCoordinatesBounds. Just add 360 to the Western Longitude.

e.g. to show Japan and Mexico: LongitudeNE: 128.43 LongitudeSW: 273.19

So you just need a React Native Wrapper for that method. I did a quick and dirty version with custom data types for my project, but have a look for some inspiration: https://github.com/jaysquared/maps/commit/7d369a77d6bd5d8cafdda6d776b81e982f4d48a5

To calculate out of an array of Coordinates / CoordinateBounds I use this method:

const generateVisibleBounds = (mixedBounds: Array<CoordinateBoundsType | CoordinateType>): ?CoordinateBoundsType => {

  const latitudes = [];
  const longitudes = [];
  mixedBounds.forEach((mixedBound) => {
    if (typeof mixedBound.latitude === 'number' && typeof mixedBound.longitude === 'number') {
      latitudes.push(mixedBound.latitude);
      longitudes.push(mixedBound.longitude);
    } else {
      latitudes.push(mixedBound.sw.latitude);
      longitudes.push(mixedBound.sw.longitude);
      latitudes.push(mixedBound.ne.latitude);
      longitudes.push(mixedBound.ne.longitude);
    }
  });

  const latitudeNE = Math.max(...latitudes);
  const latitudeSW = Math.min(...latitudes);
  let longitudeNE;
  let longitudeSW;

  const eastLongitudes = longitudes.filter(longitude => longitude >= 0);
  const westLongitudes = longitudes.filter(longitude => longitude < 0);

  if (eastLongitudes.length === 0 || westLongitudes.length === 0) {
    longitudeNE = Math.max(...longitudes);
    longitudeSW = Math.min(...longitudes);
  } else {
    console.log({ eastLongitudes, westLongitudes });
    const minEastLongitude = Math.min(...eastLongitudes);
    const maxEastLongitude = Math.max(...eastLongitudes);
    const minWestLongitude = Math.min(...westLongitudes);
    const maxWestLongitude = Math.max(...westLongitudes);

    const longitudeSpreadDefault = maxEastLongitude - minWestLongitude;
    const longitudeSpreadDateline = 360 - minEastLongitude + maxWestLongitude;

    if (longitudeSpreadDateline < longitudeSpreadDefault) {
      longitudeNE = minEastLongitude;
      longitudeSW = 360 + maxWestLongitude;
    } else {
      longitudeNE = maxEastLongitude;
      longitudeSW = minWestLongitude;
    }
  }


  if (!latitudeNE || !latitudeSW || !longitudeNE || !longitudeSW) {
    return null;
  }
  return {
    sw: {
      latitude: latitudeSW,
      longitude: longitudeSW,
    },
    ne: {
      latitude: latitudeNE,
      longitude: longitudeNE,
    },
  };
};
1reaction
ferdicuscommented, Sep 25, 2020

Reopening this, as it’s a legitimate issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

setBounds zooms to incorrect location when over antemeridian
Instead it should zoom the map to the area crossing the antemeridian (line at +-180 lon). ... Versions (please complete the following information):....
Read more >
JComponent.setBounds the last JComponent in the wrong place
I don't see a layout being set on the Frame, so I assume it has the default one, but the swingPanel is clearly...
Read more >
avarice - OSCHINA - 中文开源技术交流社区
Let's talk about the demand side first. Most software is not sold in boxes, available on the Internet, or downloaded from the App...
Read more >
Full text of "Significant etymology; or, Roots, stems, and ...
This word, which includes all things both in the heavens and on the earth, ... of God and divine things {theos) ; zoology,...
Read more >
dictionary.txt - Eclipse Git repositories
... align setbounds setcolor setfont setheight setindentation setleftmargin ... liter liters localization localization's localizations localize localized ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found