下面介绍在一个简单的 HTML 文件中如何使用 Openlayers 构建一个简单的地图。

    1. <!doctype html>
    2. <html lang="en">
    3. <head>
    4. <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.2.1/css/ol.css"
    5. type="text/css">
    6. <style>
    7. .map {
    8. height: 400px;
    9. width: 100%;
    10. }
    11. </style>
    12. <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.2.1/build/ol.js"></script>
    13. <title>OpenLayers example</title>
    14. </head>
    15. <body>
    16. <h2>My Map</h2>
    17. <div id="map" class="map"></div>
    18. <script type="text/javascript">
    19. var map = new ol.Map({
    20. target: 'map',
    21. layers: [
    22. new ol.layer.Tile({
    23. source: new ol.source.OSM()
    24. })
    25. ],
    26. view: new ol.View({
    27. center: ol.proj.fromLonLat([37.41, 8.82]),
    28. zoom: 4
    29. })
    30. });
    31. </script>
    32. </body>
    33. </html>