leaflet循环调用snakeIn报错

在使用L.Polyline.SnakeAnim.js创建轨迹线时候,循环调用path.snakeIn()会报错:

Unhandled Rejection (TypeError): coordinates must be finite numbers
 

解决办法:源码中异步处理

源码

snakeIn: function () {
        if (this._snaking) {
            return;
        }

        if (!('performance' in window) || !('now' in window.performance) || !this._map) {
            return;
        }

        this._snaking = true;
        this._snakingTime = performance.now();
        this._snakingVertices = this._snakingRings = this._snakingDistance = 0;

        if (!this._snakeLatLngs) {
            this._snakeLatLngs = L.LineUtil.isFlat(this._latlngs) ? [this._latlngs] : this._latlngs;
        }

        // Init with just the first (0th) vertex in a new ring
        // Twice because the first thing that this._snake is is chop the head.
        // 解决循环调用snakeIn报错问题 Unhandled Rejection (TypeError): coordinates must be finite numbers
          this._latlngs = [[this._snakeLatLngs[0][0], this._snakeLatLngs[0][0]]];
            this._update();
            this._snake();
            this.fire('snakestart');
        return this;
    },

改动

snakeIn: function () {
        if (this._snaking) {
            return;
        }

        if (!('performance' in window) || !('now' in window.performance) || !this._map) {
            return;
        }

        this._snaking = true;
        this._snakingTime = performance.now();
        this._snakingVertices = this._snakingRings = this._snakingDistance = 0;

        if (!this._snakeLatLngs) {
            this._snakeLatLngs = L.LineUtil.isFlat(this._latlngs) ? [this._latlngs] : this._latlngs;
        }

        // Init with just the first (0th) vertex in a new ring
        // Twice because the first thing that this._snake is is chop the head.
        // 解决循环调用snakeIn报错问题 Unhandled Rejection (TypeError): coordinates must be finite numbers
        setTimeout(() => {
            this._latlngs = [[this._snakeLatLngs[0][0], this._snakeLatLngs[0][0]]];
            this._update();
            this._snake();
            this.fire('snakestart');
        });
        return this;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Flask中连接前端Leaflet,您需要使用Flask的模板引擎来呈现HTML页面并从后端传递数据到前端。以下是一个简单的示例: 1. 在Flask应用程序中,创建一个路由来处理请求并将数据传递给HTML页面: ```python from flask import Flask, render_template app = Flask(__name__) @app.route('/') def index(): data = {"lat": 40.7128, "lng": -74.0060} return render_template('index.html', data=data) ``` 2. 在Flask应用程序的模板文件夹中,创建一个名为index.html的HTML文件,使用模板引擎呈现数据并使用Leaflet库创建地图: ```html <!DOCTYPE html> <html> <head> <title>Flask and Leaflet Example</title> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Include Leaflet CSS and JS files --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/leaflet/1.3.1/leaflet.css" /> <script src="https://cdn.jsdelivr.net/leaflet/1.3.1/leaflet.js"></script> <style> #mapid { height: 400px; } </style> </head> <body> <div id="mapid"></div> <script> var data = {{ data|tojson|safe }}; var mymap = L.map('mapid').setView([data.lat, data.lng], 13); L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' + '<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>', maxZoom: 18, id: 'mapbox.streets' }).addTo(mymap); </script> </body> </html> ``` 3. 运行Flask应用程序并访问http://localhost:5000/,您应该会看到一个包含地图的页面。在这个例子中,我们使用了Flask的模板引擎将数据传递给HTML页面,并使用Leaflet库创建了一个简单的地图。 这只是一个简单的示例,您可以根据自己的需求进行调整。您可以使用JavaScript代码来创建交互式地图,也可以从后端传递更多的数据到前端
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值