openlayers 點擊添加標記(要素)動態效果 Demo(可直接運行)


<!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>Document</title>
</head>
<body>
  <div id="map"></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 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
      });
      map.addLayer(vectorLayer);

      //矢量標注樣式設置函數,設置image為圖標ol.style.Icon
      function createLabelStyle(feature){
          console.log(feature);
          return new ol.style.Style({
              // image: new ol.style.Icon({
              //     anchor: [0.5, 60],              //錨點
              //     anchorOrigin:'top-right',       //錨點源
              //     anchorXUnits: 'fraction',       //錨點X值單位
              //     anchorYUnits: 'pixels',         //錨點Y值單位
              //     offsetOrigin: 'top-right',      //偏移原點
              //     opacity: 0.75,
              //     scale: 0.05,
              //     src: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1594359193211&di=1d6bb819a5daa724ff65cc4d011d4d42&imgtype=0&src=http%3A%2F%2Fku.90sjimg.com%2Felement_pic%2F17%2F10%2F27%2F05dc60e54e3aa5d093cdc32611303643.jpg'  //圖標的URL
              // }),
              text: new ol.style.Text({
                  textAlign: 'center',            //位置
                  textBaseline: 'middle',         //基准線
                  font: 'normal 14px 微軟雅黑',    //文字樣式
                  text: feature.get('name'),      //文本內容
                  fill: new ol.style.Fill({       //文本填充樣式(即文字顏色)
                      color: '#000'
                  }),
                  stroke: new ol.style.Stroke({
                      color: '#F00', 
                      width: 2
                  })
              })
          });
      }

      map.on('click', function(evt){
          var coordinate = evt.coordinate;        //鼠標單擊點的坐標
          //新建一個要素ol.Feature
          var newFeature = new ol.Feature({
              geometry: new ol.geom.Point(coordinate),  //幾何信息
              name: '標注點'
          });
          newFeature.setStyle(createLabelStyle(newFeature));      //設置要素樣式
          vectorSource.addFeature(newFeature);
      });
  </script>
</body>
</html>

樣式使用img時(注釋text部分)效果圖:

樣式使用text時(注釋image部分)效果圖:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM