index.html
<!DOCTYPE html>
<html>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>3D - Set geom altitude</title>
  <style type="text/css">
    html,body{margin:0px;height:100%;width:100%}
    .container{width:100%;height:100%}
    .panel{border-radius:5px;padding:10px;position:absolute;z-index:1;right:10px;top:10px;border:1px solid gray;background-color:white}
    .panel .item{line-height:40px}
    .text-marker{border:1px solid gray;background-color:white;font-size:30px;text-shadow:2px 0 gray;padding:15px}
  </style>
  <link rel="stylesheet" href="https://unpkg.com/maptalks/dist/maptalks.css">
  <script type="text/javascript" src="https://unpkg.com/maptalks/dist/maptalks.min.js"></script>
  <body>
    <div class="panel">

        <div class="item">
            <label>altitude</label>
            <input type="range" id="altitude" min="0" max="100" step="1" value="0">
        </div>

    </div>
    <div id="map" class="container"></div>
    <script>

      var baseLayer = new maptalks.TileLayer('base', {
        urlTemplate: 'https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png',
        subdomains: ["a","b","c","d"],
        attribution: '&copy; <a href="http://osm.org">OpenStreetMap</a> contributors, &copy; <a href="https://carto.com/">CARTO</a>'
      });
      var map = new maptalks.Map('map', {
        center: [114.26012989831725, 30.616193225646924],
        zoom: 18,
        pitch: 60,
        attribution: true,
        zoomControl: true,
        baseLayer: baseLayer
      });
      map.on('click', function (e) {
        console.log(e);
      });

      var offset = 0.001;
      var center = map.getCenter();
      var c1 = center.add(-offset, 0);
      var c2 = center.add(offset, 0);

      var c3 = center.add(offset / 2, offset / 2);

      var c11 = c1.add(0, offset);
      var c22 = c2.add(0, offset);

      c1 = c1.toArray();
      c2 = c2.toArray();
      c3 = c3.toArray();
      c11 = c11.toArray();
      c22 = c22.toArray();

      var layer = new maptalks.VectorLayer('layer', {
        enableAltitude: true
      }).addTo(map);

      var polygon = new maptalks.Polygon([[c11, c22, c3]]);
      layer.addGeometry(polygon);
      var line = new maptalks.LineString([c1, c2]);
      layer.addGeometry(line);

      var point = new maptalks.Marker(c1);
      layer.addGeometry(point);


      var uiMarker = new maptalks.ui.UIMarker(c2, {
        content: '<div class="text-marker">maptalks</div>',
        verticalAlignment: 'top'
      });
      uiMarker.addTo(map);

      function getEle(selector) {
        if (document.querySelector) {
          return document.querySelector(selector);
        }
        return document.getElementById(selector.substring(1, Infinity));
      }

      function on(ele, type, hanlder) {
        ele.addEventListener(type, hanlder);
      }

      on(getEle('#altitude'), 'change', function (e) {
        [point, uiMarker, line, polygon].forEach(function (geo) {
          geo.setAltitude(parseFloat(e.target.value));
        });
      });


    </script>
  </body>
</html>
Copied!