openlayers点击地图图标,图标跳动 动画Demo实现 (复制内容至html文件可查看效果)

1. 点击地图外dom实现

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="https://openlayers.org/en/v5.3.0/css/ol.css" />
  <script type="text/javascript" src="https://openlayers.org/en/v5.3.0/build/ol.js"></script>
  <title>openlayers 标记点闪烁</title>
</head>
<body>
  <div id="map"></div>
  <div id="baidu"><img src="https://www.baidu.com/img/flexible/logo/pc/result@2.png"></div>
  <script>
    var beijing = ol.proj.fromLonLat([116.28, 39.54]);
    var map = new ol.Map({
      target: 'map',
      layers: [
        new ol.layer.Tile({
          source: new ol.source.OSM()
        })
      ],
      view: new ol.View({
        center: beijing,
        zoom: 4
      })
    });

    var tip = document.getElementById('baidu');

    var overlay = new ol.Overlay({
      element: tip,
      positioning: 'center-center'
    });
    overlay.setPosition(beijing);
    map.addOverlay(overlay)
    let flag = true
    let animate = null

    tip.onclick = () => {
      clearInterval(animate)
      animate = setInterval(() => {
        flag = !flag
        let po = flag ? '10px' : '-10px'
        tip.style.top = po
      }, 500);
    }
  </script>
</body>
<style>
  #baidu {
    width: 100px;
    position: absolute;
    top: 10px;
  }
  img {
    width: 100%;
  }
</style>
</html>

2. 点击地图内features实现

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="https://openlayers.org/en/v5.3.0/css/ol.css" />
  <script type="text/javascript" src="https://openlayers.org/en/v5.3.0/build/ol.js"></script>
  <title>openlayers 标记点闪烁</title>
</head>
<body>
  <div id="map"></div>
  <div id="baidu"><img src="https://www.baidu.com/img/flexible/logo/pc/result@2.png"></div>
  <script>
    var beijing = ol.proj.fromLonLat([116.28, 39.54]);
    var dot = ol.proj.fromLonLat([103.28, 39.34]);
    var map = new ol.Map({
      target: 'map',
      layers: [
        new ol.layer.Tile({
          source: new ol.source.OSM()
        })
      ],
      view: new ol.View({
        center: beijing,
        zoom: 4
      })
    });
    //实例化矢量点要素,通过矢量图层添加到地图容器中
    //这样就实现了预先加载图文标注
    var iconFeature = new ol.Feature({
      geometry: new ol.geom.Point(beijing),
      name: '北京市',                         //名称属性
      population: 2115                       //人口数(万)
    });
    //设置点要素样式
    iconFeature.setStyle(createLabelStyle(iconFeature));
    //矢量标注的数据源
    var vectorSource = new ol.source.Vector({
      features: [iconFeature]
    });
    //矢量标注图层
    var vectorLayer = new ol.layer.Vector({
      source: vectorSource,
      visible: true
    });

    map.addLayer(vectorLayer);
    //矢量标注样式设置函数,设置image为图标ol.style.Icon
    function createLabelStyle(feature) {
      console.log(feature);
      return new ol.style.Style({
        text: new ol.style.Text({
          text: '北京',
          font: "normal 20px 微软雅黑", // 文字样式
        }),
      });
    }
    var tip = document.getElementById('baidu');
    var overlay = new ol.Overlay({
      element: tip,
      positioning: 'center-center'
    });
    overlay.setPosition(dot);
    map.addOverlay(overlay)

    let animate = null
    let select = new ol.interaction.Select();
    map.addInteraction(select)
    let flag = true
    select.on('select', (e) => {
      clearInterval(animate)
      animate = setInterval(() => {
        flag = !flag
        let po = flag ? '10px' : '-10px'
        tip.style.top = po
      }, 800);
    })
  </script>
</body>
<style>
  #baidu {
    width: 100px;
    position: absolute;
    top: 10px;
  }
  img {
    width: 100%;
  }
</style>
</html>
posted @ 2021-11-08 18:14  wwj007  阅读(1287)  评论(3编辑  收藏  举报
……