Mapbox Geojson在角度

发布于 2025-02-09 15:33:35 字数 3596 浏览 3 评论 0原文

我正在尝试使用Angular中的MAPBOX在地图中添加多边形。 我正在关注本教程: https://docs.mapbox.com/mapbox.com/mapbox-gl-js /示例/geojson-polygon/

我正在添加一个包含geojson数据的数据源,然后添加一个新图层以可视化多边形。

这是我的代码:

    const map = new mapboxgl.Map({
        accessToken: 'pk.eyJ1IjoibWF1cm8xNjE4IiwiYSI6ImNsNGdicm85bTA1NXIzam85ZWhoYWhuNjAifQ.YJ8qpHlaf5HyhztyhMmPTQ',
        container: 'map',
        style: this.style,
        zoom: 5,
        center: [-68.137343, 45.137451]
    });

    map.on('load', () => {
        // Add a data source containing GeoJSON data.
        map.addSource('maine', {
            'type': 'geojson',
            'data': {
                'type': 'Feature',
                'geometry': {
                    'type': 'Polygon',
                    // These coordinates outline Maine.
                    'coordinates': [
                        [
                            [-67.13734, 45.13745],
                            [-66.96466, 44.8097],
                            [-68.03252, 44.3252],
                            [-69.06, 43.98],
                            [-70.11617, 43.68405],
                            [-70.64573, 43.09008],
                            [-70.75102, 43.08003],
                            [-70.79761, 43.21973],
                            [-70.98176, 43.36789],
                            [-70.94416, 43.46633],
                            [-71.08482, 45.30524],
                            [-70.66002, 45.46022],
                            [-70.30495, 45.91479],
                            [-70.00014, 46.69317],
                            [-69.23708, 47.44777],
                            [-68.90478, 47.18479],
                            [-68.2343, 47.35462],
                            [-67.79035, 47.06624],
                            [-67.79141, 45.70258],
                            [-67.13734, 45.13745]
                        ]
                    ]
                }
            }
        });

        // Add a new layer to visualize the polygon.
        map.addLayer({
            'id': 'maine',
            'type': 'fill',
            'source': 'maine', // reference the data source
            'layout': {},
            'paint': {
                'fill-color': '#0080ff', // blue color fill
                'fill-opacity': 0.5
            }
        });
        // Add a black outline around the polygon.
        map.addLayer({
            'id': 'outline',
            'type': 'line',
            'source': 'maine',
            'layout': {},
            'paint': {
                'line-color': '#000',
                'line-width': 3
            }
        });
    });

'数据'以红色下划线,我会收到以下错误:

Error: src/app/modules/admin/apps/google-maps/google-maps.component.ts:157:13 - error TS2322: Type '{ type: "Feature"; geometry: { type: "Polygon"; coordinates: number[][][]; }; }' is not assignable to type 'string | Feature<Geometry, { [name: string]: any; }> | FeatureCollection<Geometry, { [name: string]: any; }>'.
  Property 'properties' is missing in type '{ type: "Feature"; geometry: { type: "Polygon"; coordinates: number[][][]; }; }' but required in type 'Feature<Geometry, { [name: string]: any; }>'.

157             'data': {
                ~~~~~~

  node_modules/@types/geojson/index.d.ts:165:5
    165     properties: P;
            ~~~~~~~~~~
    'properties' is declared here.

任何想法如何解决此问题?谢谢,我什么也没找到,谢谢。

I am trying to add a polygon in a map using MapBox in Angular.
I am following this tutorial:
https://docs.mapbox.com/mapbox-gl-js/example/geojson-polygon/

I am adding a data source containing GeoJSON data and then adding a new layer to visualize the polygon.

Here's my code:

    const map = new mapboxgl.Map({
        accessToken: 'pk.eyJ1IjoibWF1cm8xNjE4IiwiYSI6ImNsNGdicm85bTA1NXIzam85ZWhoYWhuNjAifQ.YJ8qpHlaf5HyhztyhMmPTQ',
        container: 'map',
        style: this.style,
        zoom: 5,
        center: [-68.137343, 45.137451]
    });

    map.on('load', () => {
        // Add a data source containing GeoJSON data.
        map.addSource('maine', {
            'type': 'geojson',
            'data': {
                'type': 'Feature',
                'geometry': {
                    'type': 'Polygon',
                    // These coordinates outline Maine.
                    'coordinates': [
                        [
                            [-67.13734, 45.13745],
                            [-66.96466, 44.8097],
                            [-68.03252, 44.3252],
                            [-69.06, 43.98],
                            [-70.11617, 43.68405],
                            [-70.64573, 43.09008],
                            [-70.75102, 43.08003],
                            [-70.79761, 43.21973],
                            [-70.98176, 43.36789],
                            [-70.94416, 43.46633],
                            [-71.08482, 45.30524],
                            [-70.66002, 45.46022],
                            [-70.30495, 45.91479],
                            [-70.00014, 46.69317],
                            [-69.23708, 47.44777],
                            [-68.90478, 47.18479],
                            [-68.2343, 47.35462],
                            [-67.79035, 47.06624],
                            [-67.79141, 45.70258],
                            [-67.13734, 45.13745]
                        ]
                    ]
                }
            }
        });

        // Add a new layer to visualize the polygon.
        map.addLayer({
            'id': 'maine',
            'type': 'fill',
            'source': 'maine', // reference the data source
            'layout': {},
            'paint': {
                'fill-color': '#0080ff', // blue color fill
                'fill-opacity': 0.5
            }
        });
        // Add a black outline around the polygon.
        map.addLayer({
            'id': 'outline',
            'type': 'line',
            'source': 'maine',
            'layout': {},
            'paint': {
                'line-color': '#000',
                'line-width': 3
            }
        });
    });

'data' is underlined in red and I get the following error:

Error: src/app/modules/admin/apps/google-maps/google-maps.component.ts:157:13 - error TS2322: Type '{ type: "Feature"; geometry: { type: "Polygon"; coordinates: number[][][]; }; }' is not assignable to type 'string | Feature<Geometry, { [name: string]: any; }> | FeatureCollection<Geometry, { [name: string]: any; }>'.
  Property 'properties' is missing in type '{ type: "Feature"; geometry: { type: "Polygon"; coordinates: number[][][]; }; }' but required in type 'Feature<Geometry, { [name: string]: any; }>'.

157             'data': {
                ~~~~~~

  node_modules/@types/geojson/index.d.ts:165:5
    165     properties: P;
            ~~~~~~~~~~
    'properties' is declared here.

Any ideas how to solve this?? I couldn't find nothing around, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文