使用 Lat/lon 数组 - Mapbox js 为图标设置动画

Animate an icon by using serious of Lat/lon Array - Mapbox js

我正在修改波纹管脚本以查看如何通过 Lat/lon 的数组移动图标,但它总是说这样的错误,但我提供的是数组

任何人都可以帮助我理解我做错了什么吗? 我崇敬这个例子 https://www.mapbox.com/mapbox-gl-js/example/animate-marker/

Error : 
lng_lat.js:121 Uncaught Error: `LngLatLike` argument must be specified as a LngLat instance, an object {lng: <lng>, lat: <lat>}, an object {lon: <lng>, lat: <lat>}, or an array of [<lng>, <lat>]
    at Function.yu.convert (lng_lat.js:121)
    at o.setLngLat (marker.js:251)
    at animateMarker (animate.html:33) 

修改后的代码:-

<html>
<head>
    <meta charset='utf-8' />
    <title>Animate a marker</title>
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
    <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.51.0/mapbox-gl.js'></script>
    <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.51.0/mapbox-gl.css' rel='stylesheet' />
    <style>
        body { margin:0; padding:0; }
        #map { position:absolute; top:0; bottom:0; width:100%; }
    </style>
</head>
<body>

<div id='map'></div>
<script>
mapboxgl.accessToken = '';
var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/streets-v9',
    center: [90.35388165034988, 23.725173272533567],
    zoom: 10
});

var marker = new mapboxgl.Marker();

function animateMarker() {
    var radius = 20;

    // Update the data to a new position based on the animation timestamp. The
    // divisor in the expression `timestamp / 1000` controls the animation speed.
    marker.setLngLat([
 
 [90.35388165034988, 23.725173272533567],
 [90.37379437008741, 23.732873570085644] ,
 [90.38563900508132, 23.72297310398119],
 [90.35388165034988, 23.725173272533567],
 [90.35388165034988, 23.725173272533567]
  
    ]);

    // Ensure it's added to the map. This is safe to call if it's already added.
    marker.addTo(map);

    // Request the next frame of the animation.
    requestAnimationFrame(animateMarker);
}

// Start the animation.
requestAnimationFrame(animateMarker);
</script>

</body>
</html>

您只能将一个坐标传递给 setLngLat。您不能传递数组。这是一个粗略的例子,在动画函数中,我们使用时间从点数组中选择一个位置,并将 那个位置 传递给标记。

var controlPoints = [
 [90.35388165034988, 23.725173272533567],
 [90.37379437008741, 23.732873570085644] ,
 [90.38563900508132, 23.72297310398119],
 [90.35388165034988, 23.725173272533567],
 [90.35388165034988, 23.725173272533567]
];

function animateMarker(timestamp) {
    // stay at each point for 1 second, then move to the next
    // (lower 1000 to 500 to move 2x as fast)
    var position = Math.floor(timestamp / 1000) % controlPoints.length;
    marker.setLngLat(controlPoints[position])

    // Ensure it's added to the map. This is safe to call if it's already added.
    marker.addTo(map);

    // Request the next frame of the animation.
    requestAnimationFrame(animateMarker);
}

这个动画会很粗糙。理想情况下,您将获取这些控制点并创建折线或线性环,然后您的动画函数将以设定速度(如 30 km/s)沿折线进行插值。您最终会得到一个非常漂亮的动画,它跟随控制点的路径。