简体   繁体   English

错误:类型属性必须在 mapbox-gl-js 中定义

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

I have problem in adding source to map.我在将源添加到 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
        ));

./temp-data ./临时数据

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]
              ]
          }
      }
    }
}

With the above code i am getting this error.使用上面的代码我得到了这个错误。

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

If i change如果我改变

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

to

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

then i am not getting any error.然后我没有收到任何错误。

addSource takes an object and id. addSource采用 object 和 id。 I am passing id as the first parameter and object as the second parameter.我将 id 作为第一个参数传递,将 object 作为第二个参数传递。 Then why this error is appearing.那为什么会出现这个错误。

Your error is because you are passing the function reference, not actually calling it.您的错误是因为您传递了 function 引用,而不是实际调用它。

Change this:改变这个:

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

to this:对此:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM