Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

viewport-mercator-project assertion failed #675

Closed
tmhn opened this issue Dec 5, 2018 · 28 comments
Closed

viewport-mercator-project assertion failed #675

tmhn opened this issue Dec 5, 2018 · 28 comments
Labels

Comments

@tmhn
Copy link

tmhn commented Dec 5, 2018

I realise that the title relates to another Uber project - viewport-mercator-project, however it looks rather quiet there and I'm using this package within my React app, I thought here might be a better option.

I've tried to render a Map within a Create React App project:
react v: 16.5.2
react-map-gl v: 4.0.2

When navigating to the page with the Map component, it resulted in this error in the Chrome console

0efa13d7437878dc03536a9dedddbbf1a8d59712.js:1 Error: viewport-mercator-project: assertion failed.
    at C (0efa13d7437878dc03536a9dedddbbf1a8d59712.js:1)
    at 0efa13d7437878dc03536a9dedddbbf1a8d59712.js:1
    at t.value (0efa13d7437878dc03536a9dedddbbf1a8d59712.js:1)
    at t.value (0efa13d7437878dc03536a9dedddbbf1a8d59712.js:1)
    at 0efa13d7437878dc03536a9dedddbbf1a8d59712.js:1
    at eo (0efa13d7437878dc03536a9dedddbbf1a8d59712.js:1)
    at ko (0efa13d7437878dc03536a9dedddbbf1a8d59712.js:1)
    at Io (0efa13d7437878dc03536a9dedddbbf1a8d59712.js:1)
    at pa (0efa13d7437878dc03536a9dedddbbf1a8d59712.js:1)
    at la (0efa13d7437878dc03536a9dedddbbf1a8d59712.js:1)

The React Map component:

import React, { PureComponent } from "react";
import PropTypes from "prop-types";
import { merge } from "ramda";
import MapGL, { Marker } from "react-map-gl";
import Pin from "./Pin";

class LocationMap extends PureComponent {
  static displayName = "LocationMap";
  static propTypes = {
    lat: PropTypes.number,
    lng: PropTypes.number,
    zoom: PropTypes.number,
    pinSize: PropTypes.number
  };
  static defaultProps = {
    pinSize: 20
  };

  state = {
    viewport: {
      width: "100%",
      height: 400,
      latitude: 53.795231,
      longitude: -1.54511300000001,
      zoom: 15
    }
  };

  render() {
    const { pinSize } = this.props;
    const propsData = { latitude: this.props.latitude, longitude: this.props.longitude };
    const mapConfig = merge(this.state.viewport, propsData);

    return (
      <MapGL
        mapboxApiAccessToken={process.env.REACT_APP_MAPBOX_TOKEN}
        onViewportChange={viewport => this.setState({ ...viewport, width: "100%" })}
        {...mapConfig}
      >
        <Marker latitude={this.props.latitude} longitude={this.props.longitude}>
          <Pin size={pinSize} />
        </Marker>
      </MapGL>
    );
  }
}

export default LocationMap;

Does anyone have any ideas about how to resolve this? Perhaps fixing the version of react-map-gl to "^3.." ?

@jcready
Copy link

jcready commented Dec 6, 2018

The problem is likely due to setting the viewport width to a string instead of a number.

@Pessimistress
Copy link
Collaborator

Your propTypes specify lng lat but your render method is using latitude longitude. Can you log the mapConfig object to make sure all fields are valid?

@Pessimistress
Copy link
Collaborator

@jcready Using string for width is allowed in v4.0. Although in the above code sample, the onViewportChange callback is not updating state.viewport but flattening all viewport props into state. An easier way to do the override is just

<MapGL
  mapboxApiAccessToken={process.env.REACT_APP_MAPBOX_TOKEN}
    onViewportChange={viewport => this.setState({viewport})}
    {...mapConfig}
    width="100%"
>

@tmhn
Copy link
Author

tmhn commented Dec 10, 2018

@Pessimistress Thanks for the great suggestion, I did try this, but then the map doesn't render quite properly within the parent div and breaks over from the styling of that div.
When I log out mapConfig:

altitude: 1.5
bearing: 0
height: 400
latitude: 53.79137
longitude: -1.53104
maxPitch: 60
maxZoom: 24
minPitch: 0
minZoom: 0
pitch: 0
transitionDuration: 0
transitionEasing: ƒ transitionEasing(t)
transitionInterpolator: LinearInterpolator {propNames: Array(5), around: undefined}
transitionInterruption: 1
width: "100%"
zoom: 15
__proto__: Object

Unless I'm missing something, it looks fairly standard and expected...

@tmhn
Copy link
Author

tmhn commented Dec 10, 2018

React Map GL Stack trace error

I'm running my React app on Netlify and had enabled minification, bundling and optimisation of the JS & Images. I've turned off the additional performance upgrades, and this is the console error that I've found

@gurbraj
Copy link

gurbraj commented Dec 10, 2018

am also having a similar problem!

EDIT: Never mind! Had the wrong order on (latitude, longitude).

@tmhn
Copy link
Author

tmhn commented Dec 10, 2018

@gurbraj Did you find that swapping the order of latitude & longitude successfully fixed it? Do you have a code sample to show what you've altered?

@gurbraj
Copy link

gurbraj commented Dec 10, 2018

@tmhn, Yes it seems to have fixed it on my end.

Given that you can render the map at all without any Markers, an equivalent change would be to change
const propsData = { latitude: this.props.latitude, longitude: this.props.longitude };
to
const propsData = { latitude: this.props.longitude, longitude: this.props.latitude };

(Of course, if this fixes it, the switch of ordering would come higher up = )

@shaunwarman
Copy link

shaunwarman commented Dec 29, 2018

It's important latitude doesn't go outside -90 and 90 and longitude outside -180 and 180.

@Pessimistress
Copy link
Collaborator

Invalid latitude value passed to Marker is likely causing the problem. The error message is not very helpful - I can add a sanity check.

@mightym
Copy link

mightym commented Jan 17, 2019

In my case this happens when using a string for width/height:

          width="100%"
          height="100%"

using a number instead of a string removes the error:

          width={100}
          height={100}

But sadly doesn't solve my issue because I need a 100% dimension vor height and width

@timendez
Copy link

Accidentally swapping latitude and longitude was my problem as well!

@LoicUV
Copy link

LoicUV commented Feb 28, 2019

@mightym I had the same issue when trying to fit bounds using new WebMercatorViewport(viewport) with viewport having a width: '100%' and height: '100%' (which are fed directly to <MapGL> component).

I fixed it by replacing the width and height of viewport with the current offsetWidth & offsetHeight of the parent element of the map.

I don't know if it can help you, but if it does, beware that accessing offsetWidth causes a reflow of the page (use sparingly).

@ajsharp
Copy link

ajsharp commented Apr 7, 2019

I was running into this because I was passing a null lat/lng pair. FWIW, I'm using version 6.1.1 of viewport-mercator-project, which includes uber-archive/viewport-mercator-project#91, but I was still just seeing the assertion failed error message.

@BenBach
Copy link

BenBach commented Apr 10, 2019

In my case this happens when using a string for width/height:

          width="100%"
          height="100%"

using a number instead of a string removes the error:

          width={100}
          height={100}

But sadly doesn't solve my issue because I need a 100% dimension vor height and width

Were you able to find a solution for this? Would appreciate any help

@0vidiu
Copy link

0vidiu commented May 2, 2019

Maybe it'll help someone:
I was getting this error because width and height were both 0 in some instances. Especially in shallow rendering with Enzyme.

@jovanialferez
Copy link

@tmhn i got a similar issue with you and error rooted from the Marker. In your case, seems like your marker is accessing props latitude and longitude while your component has propTypes lat and lng.

@aosvaldtRS
Copy link

I got similar issue. Just converted the latitude and longitude I've received to a number.

@romische
Copy link

had the same issue, for me it was because I was providing a padding too big compared to my width.

.fitBounds(bounds, { padding: 50 });

@alexsinfarosa
Copy link

The image below is the viewport object I feed into the WebMercatorViewport constructor

image

After reading all the above comments and made all adjustments, I am still seeing the error below.

image

@gauravverma029
Copy link

After reading all the above comments and made all adjustments, I am still seeing the error below.
you should Try this .. it is working for me before i was getting same error

const mapViewportBounds = mapViewport.fitBounds([
[parseFloat(south), parseFloat(west)],
[parseFloat(north), parseFloat(east)],
]);

@chiho13
Copy link

chiho13 commented May 19, 2020

@math.gl/web-mercator: assertion failed.
i get this when I change route or when state changes

@daf
Copy link

daf commented May 27, 2020

@chiho13 same for me - turns out I was specifying height as a string, which works fine in development, but something about the production pipeline makes it throw @math.gl/web-mercator: assertion failed.

@ArifSanaullah
Copy link

ArifSanaullah commented Jun 23, 2020

I was getting the same error. Turning Number(parseFloat(latitude)) into Number(latitude) solved the error.

@weisssean
Copy link

weisssean commented Aug 25, 2020

I also had the problem with the relative height/width so I checked them before my fitToBounds function:

  const viewportMercator = new WebMercatorViewport(viewport);
  //because we gave the map relative sizes, we have to make sure it rendered before zooming.
  if (viewportMercator.width !== 1) {
       const { longitude, latitude, zoom } = viewportMercator.fitBounds(...);

...

@roscaImarius
Copy link

I was getting the same error. Turning Number(parseFloat(latitude)) into Number(latitude) solved the error.

thank you sir!

@badiuzzamanbarat
Copy link

badiuzzamanbarat commented Dec 22, 2021

React-map-gl not supports string number **only support Number latitude longitude ** . please check your json

@emflomed17
Copy link

I also had the problem with the relative height/width so I checked them before my fitToBounds function:

  const viewportMercator = new WebMercatorViewport(viewport);
  //because we gave the map relative sizes, we have to make sure it rendered before zooming.
  if (viewportMercator.width !== 1) {
       const { longitude, latitude, zoom } = viewportMercator.fitBounds(...);

...

Did you remember why you validate viewportMercator.width !== 1 I mean, why 1 and not different from 0 or any other number.

Thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests