2020年5月31日

第一個 OpenLayers 圖台範例 - Polygon。

參考資料:


<!doctype html>
<html lang="en">
  <head>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.3.1/css/ol.css" type="text/css">
    <style>
      .map {
        height: 400px;
        width: 100%;
      }
    </style>
    <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.3.1/build/ol.js"></script>
    <title>OpenLayers example</title>
  </head>
  <body>
    <div id="map" class="map"></div>
    <script type="text/javascript">
      var map = new ol.Map({
        target: 'map',
        layers: [
          new ol.layer.Tile({
            source: new ol.source.OSM()
          })
        ],
        view: new ol.View({
          center: ol.proj.fromLonLat([37.41, 8.82]),
          zoom: 4
        })
      });


      // Your loctations
      var locations = [[120.696212,24.261705], [120.771935,24.260560], [120.710195,24.174122 ],[120.696212,24.261705]];

      var polygon = new ol.geom.Polygon([locations]);
      polygon.transform('EPSG:4326', 'EPSG:3857');
      // Create feature with polygon.
      var feature = new ol.Feature(polygon);
      // Create vector source and the feature to it.
      var vectorSource = new ol.source.Vector();
      vectorSource.addFeature(feature);
      // Create vector layer attached to the vector source.
      var vectorLayer = new ol.layer.Vector({
        source: vectorSource,
        style: new ol.style.Style({
          stroke: new ol.style.Stroke({
            color: 'rgba(100, 255, 0, 1)',
            width: 2
          }),
          fill: new ol.style.Fill({
            color: 'rgba(100, 255, 0, 0.3)'
          })
        })
      });
      // Add the vector layer to the map.
      map.addLayer(vectorLayer);
      //定位
      map.getView().setCenter(ol.proj.transform([ 120.696212,24.261705], 'EPSG:4326', 'EPSG:3857'))
      //圖台階層
      map.getView().setZoom(13);
    </script>
  </body>
</html>


沒有留言:

張貼留言