arcGis 实现单个图标跳动 正常封装方法
时间: 2023-12-06 10:02:40 浏览: 32
要实现单个图标跳动,可以使用ArcGIS API for JavaScript中的GraphicsLayer和Animation类。
首先,创建一个GraphicsLayer并将其添加到地图上:
```
var graphicsLayer = new GraphicsLayer();
map.addLayer(graphicsLayer);
```
然后,创建一个Graphic对象来表示要跳动的图标:
```
var point = new Point(longitude, latitude, map.spatialReference);
var symbol = new PictureMarkerSymbol(iconUrl, iconWidth, iconHeight);
var graphic = new Graphic(point, symbol);
graphicsLayer.add(graphic);
```
使用Animation类来实现图标跳动:
```
var animation = animateGraphic(graphic);
animation.play();
```
其中,animateGraphic函数返回一个Animation对象,该对象将图形从其当前位置移动到指定位置并在此过程中执行动画。在这种情况下,我们只需要将图标向上移动一定的距离,然后再向下移动到其原始位置即可实现跳动效果。
```
function animateGraphic(graphic) {
var animation = new Animation({
duration: 1000,
easing: "in-out-expo",
onAnimate: function (value) {
var geometry = graphic.geometry;
var newPosition = new Point(geometry.x, geometry.y + value, geometry.spatialReference);
graphic.setGeometry(newPosition);
}
});
animation.setDirection(Animation.prototype.DIRECTION_ALTERNATE);
animation.setRepeat(Animation.prototype.REPEAT_FOREVER);
return animation;
}
```
在这个函数中,我们使用onAnimate函数来更新图形的位置,然后设置动画的方向和重复次数。
最后,我们需要使用dojo/on模块来监听地图的zoom-end事件,以便在地图缩放时重新计算图标的跳动高度:
```
on(map, "zoom-end", function () {
var zoomLevel = map.getZoom();
var jumpHeight = 0.01 * zoomLevel;
animation.stop();
animation = animateGraphic(graphic, jumpHeight);
animation.play();
});
```
这样,当地图缩放时,图标的跳动高度将自动适应缩放级别。