首页 > 解决方案 > GeoJSON nested objects Mapbox GL JS

问题描述

So I am trying to plot value_max_local within this GEOJSON file. I am having trouble getting this into the text-field.

Ive tried minmax.wind_gust_value_1.value_max_local[0], and the following recommendation for expressions...

'text-field': ['number-format', ['get', 'minmax', ['get', 'wind_gust_value_1', ['get', 'value_max_local[0]']]], {'min-fraction-digits':0, 'max-fraction-digits':0.1}],

Here is the code... with a piece of the geojson file.

map.addLayer({
  'id': 'MxGusts',
  'type': 'symbol',
  'source':'Gusts',
  'layout': {
    'text-field': ['number-format', ['get', 'minmax', ['get', 'wind_gust_value_1', ['get', 'value_max_local[0]']]], {'min-fraction-digits':0, 'max-fraction-digits':0.1}],
    'text-font': [
      "DIN Offc Pro Medium",
      "Arial Unicode MS Bold"
    ],
    'text-size': 13,
  },
  'paint':{
    'text-halo-color':'rgba(255,255,255,0.75)',
    'text-halo-width':1.5,
  },
},firstSymbolId);



"geometry": {
                "type": "Point",
                "coordinates": [
                    -111.38222,
                    47.47333
                ]
            },
            "type": "Feature",
            "properties": {
                "status": "ACTIVE",
                "mnet_id": "1",
                "date_time": "2019-06-12T16:50:00Z",
                "elevation": "3675",
                "name": "Great Falls, Great Falls International Airport",
                "station_info": "http://mesowest.utah.edu/cgi-bin/droman/station_total.cgi?stn=KGTF",
                "restricted_data": "0",
                "stid": "KGTF",
                "elev_dem": "3654.9",
                "longitude": "-111.38222",
                "minmax": {
                    "wind_gust_value_1": {
                        "datetime_min_local": [
                            "2019-06-12T09:50:00"
                        ],
                        "value_min_local": [
                            13
                        ],
                        "dates": [
                            "2019-06-12"
                        ],
                        "value_max_local": [
                            13
                        ],
                        "datetime_max_local": [
                            "2019-06-12T09:50:00"
                        ]
                    }
                },
                "state": "MT",
                "more_observations": "http://mesowest.utah.edu/cgi-bin/droman/meso_base_dyn.cgi?stn=KGTF",
                "latitude": "47.47333",
                "timezone": "America/Denver",
                "id": "205",
                "wind_gust": 13
            }
        },

I am not getting the text-field to show correctly the value_max_local field.

The geojson is located here: https://api.synopticdata.com/v2/stations/latest?&token=d8c6aee36a994f90857925cea26934be&within=60&output=geojson&bbox=-125,35,-110,50&vars=wind_gust&units=english&network=1,2,5,10,14,96,106,65&qc=on&hfmetars=0&minmax=1&minmaxtype=local

标签: mapbox-gl-js

解决方案


你的表达方式不太对。您要提取的值位于数组中,因此您需要使用at表达式来提取它;简化:

{
  ...
  properties: {
    foo: {
      bar: [
        10 <-- what you want to access
      ]
    }
  }
}

表达式应如下所示:

[
  'at',
  0, // <-- returns element at index "0" from array "bar"
  [
    'get', 'bar', // <-- returns the array "bar"
    [
      'get', 'foo' // <-- returns the object "foo" from properties
    ]
  ]
]

推荐阅读