-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
isSourceLoaded returns error instead of false #9430
Comments
"Loaded" in this context means "the data for the source has been successfully retrieved", not "the source has been added to the map object". I think it's behaving as expected. FWIW, I find the behaviour of throwing errors when trying to remove layers/sources that don't exist a bit annoying. My utility library mapbox-gl-utils suppresses them. (It also automatically removes layers from sources). |
Thanks for your explanation @stevage, I think I now understand how the method is actually meant. In the documentation, it simply says:
I think i was not aware that a difference between sources and layers exist. It's difficult to say what to do, but form my point of view there's three possibilities:
I agree on your view regarding the annoyance of errors being thrown in this case. For now, this is still fine for me since the application it was used in was more a proof-of-concept. |
It is true that this (and other similar states and events) are unclearly worded, and this can be a real pain point. See also #6707 and #6314.
There is |
If all you want to do is determine if a layer is visible within the current viewport I think the error handling issue is valid and we can think about whether or not we should fix that. We're currently researching and thinking about our error handling so any examples of pain points are helpful. If you need more assistance specifically with detecting a "loaded" layer, an example would help provide more insight @nsteffens. Even if you can't use the specific implementation from your app, a minimal, analogous example would help. |
Thanks to both of you, let me do a bit of explanation of my use-case to provide some more Information. They map I'm creating is, after some internal calculations, fetching a custom geojson polygon from an internal API. This is added as a source with the id Using the methods you mentioned above, I've checked the following on a blank map:
To me, it seems getLayer is most suitable for this as I just have to check that it's not In general, I basically do not need assistance on this. Due to this code only being used in an internal prototype, it's not that big of a problem. I just wanted to see if this "throw an error where I'd expect an boolean"-issue was known. Thanks for adding it to the master ticket! IMO this issue can be closed. |
@nsteffens You can guard your call to |
Hi there,
I'm using the mapbox-gl-js framework to display a polygon a map. The setup is quite simple, for licensing reasons i'm not allowed to publish the whole code.
See this code snippet:
if (map.isSourceLoaded('isochrone')) { map.removeLayer('isochroneLayer'); map.removeSource('isochrone'); }
This somewhat works fine, however, isSourceLoaded is returning an error if the source is not loaded:
I'd expect the function to simply return false instead of an error. Is this a bug or expected behavior?
The text was updated successfully, but these errors were encountered: