首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >接收'Unhandled Rejection‘错误

接收'Unhandled Rejection‘错误
EN

Stack Overflow用户
提问于 2021-09-13 01:51:18
回答 1查看 108关注 0票数 0

当我在我的MapBox地图上创建一个活动的跟踪线时,我收到了这个错误。

我不得不在这个文件的其他地方使用相同的api。我猜这就是问题所在..有没有办法解决这个问题?

错误-

代码语言:javascript
复制
Error: There is already a source with this ID
    at i.addSource (mapbox-gl.js?e192:33)
    at r.addSource (mapbox-gl.js?e192:33)
    at VueComponent._callee5$ (Dashboard.vue?98fa:142)
    at tryCatch (runtime.js?96cf:45)
    at Generator.invoke [as _invoke] (runtime.js?96cf:271)
    at Generator.prototype.<computed> [as next] (runtime.js?96cf:97)
    at asyncGeneratorStep (asyncToGenerator.js?3b8d:5)
    at _next (asyncToGenerator.js?3b8d:27)

已挂载呼叫-

代码语言:javascript
复制
  mounted() {
    this.createMap();
      window.setInterval(() => {
        this.getLocation()
      }, 500)
  },

第一个API调用-

代码语言:javascript
复制
async getLocation() {
  const res = await getLocations()
  this.center = [res.data[0].longitude,res.data[0].latitude];

  // Reverse Geocoding using MapBox API
  axios.get(`https://api.mapbox.com/geocoding/v5/mapbox.places/${this.center}.json?access_token=${this.access_token}`)
      .then(async (response) => {
         this.loading = false;
         this.lat = response.data.query[1].toFixed(6);
         this.lng = response.data.query[0].toFixed(6);
         this.place_name = response.data.features[0].place_name;
         this.uncertainty_radius = res.data[0].uncertaintyRadiusInMeters;
         this.map.flyTo({center: [this.lng, this.lat], zoom: 16.5});
      });
  await this.mapMarker();
  await this.targetTrackingLine()
},

这是我的以下行函数-

代码语言:javascript
复制
async targetTrackingLine() {
  if (this.lng !== "0.000000" && this.lng !== "") {
    const response = await fetch(
        `https://api.mapbox.com/geocoding/v5/mapbox.places/${this.center}.json?access_token=${this.access_token}`
    );
    const data = await response.json();
    // save full coordinate list for later
    const coordinates = data.query;
    console.log(coordinates)
    // start by showing just the first coordinate
    data.query.coordinates = [coordinates[0]];
    // add it to the map
    this.map.addSource('trace', {type: 'geojson', data: data});
    this.map.addLayer({
      'id': 'trace',
      'type': 'line',
      'source': 'trace',
      'paint': {
        'line-color': '#000',
        'line-width': 4
      }
    });
    // on a regular basis, add more coordinates from the saved list and update the map
    let i = 0;
    const timer = setInterval(() => {
      if (i < coordinates.length) {
        data.query.coordinates.push(coordinates[i]);
        this.map.getSource('trace').setData(data);
        i++;
      } else {
        window.clearInterval(timer);
      }
    }, 10);
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-13 05:27:01

下面的代码创建一个新的id,以便在每次调用targetTrackingLine时传递给map.addSource

代码语言:javascript
复制
let targetTrackingCalls = 0;
async targetTrackingLine() {
   const sourceId = "trace" + ++targetTrackingCalls; // unique per call

   //... source lines
   this.map.addSource(sourceId, {type: 'geojson', data: data});

   //... in timer callback;

      this.map.getSource(sourceId).setData(data);

    //...
}

使用唯一的id应防止addSource引发的错误,始终假定data是每个MapBox specification的有效geojason对象

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69156463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档