简体   繁体   English

Mapbox GL setData 更新图层

[英]Mapbox GL setData to update layer

In my angular app I am using directions api and trying to add a route path from one direction to another.在我的 angular 应用程序中,我正在使用方向 api 并尝试添加从一个方向到另一个方向的路线路径。 On the first time when making the ajax request route path is creating properly but from the second time i can not see the route path.第一次制作 ajax 请求路由路径时创建正确,但从第二次开始我看不到路由路径。

I am getting this error from second time onwards of the ajax request - Layer with id "route" already exists on this map从 ajax 请求开始,我第二次收到此错误 - 此 map 上已存在 ID 为“路由”的层

Is there any way to update the source and layer in mapbox?有没有办法更新mapbox中的源和图层?

drawRoute() {
const url = `https://api.mapbox.com/directions/v5/mapbox/driving/${this.startData?.lng},${this.startData?.lat};${this.endData?.lng},${this.endData?.lat}?alternatives=true&geometries=geojson&steps=true&access_token=${environment.mapbox.accessToken}`;

this._http.get(url).subscribe({
    next: (result) => {
        const geojson: any = {
            type: 'Feature',
            properties: {},
            geometry: {
               type: 'LineString',
               coordinates: result.routes[0]
            }
        };

        if (this.map?.getSource('route')) {
            const source: mapboxgl.GeoJSONSource = this.map?.getSource('route') as 
             mapboxgl.GeoJSONSource;
            source.setData(geojson);
        } else {
            this.map?.addSource('route', {
            type: 'geojson',
                data: {
                    type: 'Feature',
                    properties: {},
                    geometry: {
                       type: 'LineString',
                       coordinates: result.routes[0].geometry.coordinates
                    }
                }
            });
        }

        this.map?.addLayer({
            id: 'route',
            type: 'line',
            source: 'route',
            layout: {
                'line-join': 'round',
                'line-cap': 'round'
            },
            paint: {
              'line-color': '#1F5ED8',
              'line-width': 2
            }
        });
    },
    error: (err) => {} 
})
}

I think that the setData() method that is available for GeoJSON sources in Mapbox GL JS is what you are looking for.我认为您正在寻找可用于 Mapbox GL JS 中 GeoJSON 源的setData()方法。 The method allows you to update the underlying source data and triggers a map re-render.该方法允许您更新基础源数据并触发 map 重新渲染。 The data-driven styling should then kick in and style your updates layers as desired.然后,数据驱动的样式应该会启动并根据需要为您的更新层设置样式。

https://docs.mapbox.com/mapbox-gl-js/api/sources/#geojsonsource#setdata https://docs.mapbox.com/mapbox-gl-js/api/sources/#geojsonsource#setdata

Here is a pseudo-code example这是一个伪代码示例

map.getSource("source-id").setData(updatedGeoJSONData);

Hope this helps.希望这可以帮助。 I have been writing a series of guides for Mapbox that you might be interested in too: Here are some links:我一直在为您可能感兴趣的 Mapbox 编写一系列指南:以下是一些链接:

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

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