繁体   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