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

.getStyle is broken for Standard style #13160

Open
korywka opened this issue Apr 19, 2024 · 3 comments
Open

.getStyle is broken for Standard style #13160

korywka opened this issue Apr 19, 2024 · 3 comments

Comments

@korywka
Copy link
Contributor

korywka commented Apr 19, 2024

According to MapBox JS API docs, getStyle method returns Style JSON:

image

There is not any mention or hint about Standard style, for which this method returns different:

image

This makes harder to retrieve current style name or url property.
Some developers even think that Standard style doesn't have name at all: korywka/mapbox-controls#55.

Current fix looks like this:

getCurrentStyleName() {
	if (!this.map) throw Error('map is undefined');
	/** @type {string} */
	let name;
	/** @type {any} mapbox standard style doesn't return JSON object */
	const style = this.map.getStyle();
	if (Array.isArray(style.imports) && style.imports.length) {
		// mapbox standard style
		name = style.imports[0].data.name;
		console.log(style);
	} else {
		// classic style
		name = style.name;
	}
	if (!name) throw Error('style must have name');
	return name;
}

My proposal:

  • fix documentation, add information about Standard style
  • fix JSDoc for .getStyle() documentation
  • add method to get real current style source
image

let it be string for url like mapbox://styles/mapbox/satellite-streets-v12 or mapbox://styles/mapbox/standard and Object for plain JSON style.

Or suggest please another way to identify loaded style among others.

@joshkautz
Copy link

Adding a +1. This is a well-documented report. I agree that this new Standard Style introduces some breaking changes/inconsistencies. The workaround above was helpful and I'm good to go moving forward, but I expect this to cause more people some headaches.

@stepankuzmin
Copy link
Contributor

Hi everyone 👋

Thanks for reaching out and sorry for any confusion. I understand the frustration when things don't work as expected. With the new Mapbox Standard style, we're introducing a different approach to handling map styles. When you use this style in your application, we take care of updating the basemap with the latest features automatically, so your users always see the most up-to-date version without any extra effort from your side.

For the Mapbox Standard style or any "fragment" style (those with fragment: true or a schema property defined), map.getStyle() returns an empty style with no layers or sources. The original style is encapsulated in an import with the ID basemap and isn't meant to be accessed directly. This design ensures your application logic isn't dependent on style internals, which allows us to provide seamless and consistent updates.

I’ll update the related documentation to clarify this. We don't plan to add an API for accessing import internals directly. However, I'd love to understand more about your use case. @korywka, could you please share why you need to get the import's name from the style JSON?

@korywka
Copy link
Contributor Author

korywka commented May 17, 2024

Hi.
It is necessary for Style Switcher control. The control should not only to switch style, but to indicate the current selected style.
Please give a hint If it is possible to do in other way.
The code of current implementation is here: https://github.com/korywka/mapbox-controls/blob/master/packages/styles/src/index.js

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

No branches or pull requests

3 participants