簡體   English   中英

錯誤:類型屬性必須在 mapbox-gl-js 中定義

[英]Error: The type property must be definedn in mapbox-gl-js

我在將源添加到 map 時遇到問題。

import * as data from "./temp-data";
 map.addSource('source__demo__empty',data.getEmptySource);
        map.addLayer(data.getLayer(false,'source__demo__empty','source__demo__empty',
            'green',true
        ));

./臨時數據

export const getLayer=(hovered,layerId,sourceId,color,activeRoute)=>{
    const layer = {
        'id': layerId,
        'type': 'line',
        'source': sourceId,
        'layout': {
            'line-join': 'round',
            'line-cap': 'round'
        },
        'paint': {
            'line-color': color,
            'line-width': 4,
            'line-dasharray':[1,2,3],
            'line-opacity':activeRoute==true?1:0.5
        }
      }

      return layer
}

export function getEmptySource(){
    return {
      'type':'geojson',
      'data':{
          'type':'Feature',
          'properties':{},
          'geometry':{
              'type':'LineString',
              'coordinates':[
                  [76.993894,31.781929]
              ]
          }
      }
    }
}

使用上面的代碼我得到了這個錯誤。

Error: The type property must be defined, but only the following properties were given: .

mapbox-gl.js:35 Uncaught Error: The type property must be defined, but only the following properties were given: .
    at i.addSource (mapbox-gl.js:35)
    at r.addSource (mapbox-gl.js:35)
    at r.<anonymous> (DynamicRoute.jsx:8)
    at r.zt.fire (mapbox-gl.js:31)
    at r._render (mapbox-gl.js:35)
    at mapbox-gl.js:35

如果我改變

map.addSource('source__demo__empty',data.getEmptySource);

map.addSource('source__demo__empty', {
            'type':'geojson',
            'data':{
                'type':'Feature',
                'properties':{},
                'geometry':{
                    'type':'LineString',
                    'coordinates':[
                        [76.993894,31.781929]
                    ]
                }
            }
          });

然后我沒有收到任何錯誤。

addSource采用 object 和 id。 我將 id 作為第一個參數傳遞,將 object 作為第二個參數傳遞。 那為什么會出現這個錯誤。

您的錯誤是因為您傳遞了 function 引用,而不是實際調用它。

改變這個:

map.addSource('source__demo__empty',data.getEmptySource);

對此:

map.addSource('source__demo__empty',data.getEmptySource());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM