使用严重的纬度/经度数组对图标进行动画处理-Mapbox js

时间:2018-12-12 16:11:56

标签: javascript mapbox mapbox-gl-js mapbox-gl

我正在修改风箱脚本,以了解如何按照纬度/经度数组移动图标,但它始终会显示诸如此类的错误,但我是以数组的形式提供的

有人可以帮助我了解我做错了什么吗? 我尊敬这个例子 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>

1 个答案:

答案 0 :(得分:0)

您只能将一个坐标传递给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)沿折线进行插值。您将得到一个非常漂亮的动画,该动画遵循控制点的路径。