mapbox / mapbox/mapbox-gl-js

`.getStyle` is broken for Standard style

Open
#13,160 16 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

auto-triaged bug :lady_beetle:
Dominant language
TypeScript
Stars
12.4k
Forks
2.4k
PR merge metrics
No merged PRs in 30d

Description

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: https://github.com/korywka/mapbox-controls/pull/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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the getStyle() API documentation and its Standard-style return shape, comparing it with the classic style shape described in the issue. Determine whether the intended change is documentation, JSDoc, or a new way to identify the loaded style; done should include a clearly documented and consistent result for Standard and plain JSON styles.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
api, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.