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

text-offset data-driven cause a warning when click ? #13164

Open
liuben-team opened this issue Apr 25, 2024 · 2 comments
Open

text-offset data-driven cause a warning when click ? #13164

liuben-team opened this issue Apr 25, 2024 · 2 comments

Comments

@liuben-team
Copy link

mapbox-gl-js version: 3.2.0

Question

i got a warning when i click a label use text-offset data-driven:

Expected value to be of type array<number, 2>, but found string instead.

i couldn't fix it and console the target feature, find that the properties.offset was converted to a string

so is that what caused the warning? and why?

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>demo</title>
  <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
  <link href="https://api.mapbox.com/mapbox-gl-js/v3.0.1/mapbox-gl.css" rel="stylesheet">
  <script src="https://api.mapbox.com/mapbox-gl-js/v3.0.1/mapbox-gl.js"></script>
  <style>
    body {
      margin: 0;
      padding: 0;
    }

    #map {
      position: absolute;
      top: 0;
      bottom: 0;
      width: 100%;
    }
  </style>
</head>

<body>
  <div id="map"></div>
  <script>
    // mapboxgl.accessToken = '<KEY>';
    const map = new mapboxgl.Map({
      container: 'map',
      center: [-77.4144, 25.0759],
      zoom: 15,
      style: 'mapbox://styles/mapbox/streets-v12',
    });

    map.on('load', () => {
      map.addSource('point', {
        'type': 'geojson',
        'data': {
          'type': 'FeatureCollection',
          'features': [
            {
              'type': 'Feature',
              properties: {
                text: 'Hello, world!',
                offset: [1, -1],
              },
              'geometry': {
                'type': 'Point',
                'coordinates': [-77.4144, 25.0759]
              }
            }
          ]
        }
      });

      map.addLayer({
        id: 'point',
        type: 'symbol',
        source: 'point',
        layout: {
          "text-field": "{text}",
          "text-size": 16,
          "text-offset": ['get', 'offset'],
        }
      })

      map.on('click', 'point', function (e) {
        console.log(e.features[0].properties); // offset was converted to "[1,-1]"
      })
    })
  </script>

</body>

</html>

Links to related documentation

@ticky-xia
Copy link

may be you can try the key words "literal" in expressionshttps://docs.mapbox.com/style-spec/reference/expressions#types-literal

@liuben-team
Copy link
Author

may be you can try the key words "literal" in expressionshttps://docs.mapbox.com/style-spec/reference/expressions#types-literal

"literal" treats ['get', 'offset'] as literal array rather than data expression

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

2 participants