传单 - 我似乎无法让基本示例发挥作用

Jak*_*keJ 3 html javascript leaflet

我一直在尝试让 Leaflet(一个网络地图 API)工作几个小时。起初我犯了一个错误,试图做太多事情,现在我只是试图让基本的例子发挥作用。

这是我的代码(HTML 和 Javascript):

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <script src="./leaflet.js"></script>
        <link rel="stylesheet" type="text/css" href="./leaflet.css" />  
        <script type="text/javascript">
            function initmap(){
                var map1 = L.map('map');
                //map1.setView([38.642433, -90.255372]),9); //Thanks!
                map1.setView([38.642433, -90.255372],9);

                // add an OpenStreetMap tile layer
                L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
                        attribution: '&amp;copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
                }).addTo(map1);
            }
        </script>
       <style>
            #map {
              position: absolute;
              top: 0;
              right: 0;
              left: 0;
              bottom: 0;
              top: 0;
              z-index: 0;
            }
    </style>
        <!-- Without this styling to set the map dimension, the tiles will get downloaded, but nothing will display on the map div!  Thank you choz for answering this question!  -->
    </head>

    <body onload="initmap()">
        <div id='map'></div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

摘要:我首先得到“missing ; before statements”和“Reference Error: initmap not Defined”。根据 choz 的第一条评论,通过删除地图定义中的额外括号来解决这个问题。然后我遇到了地图未显示的问题,即使正在下载图块。Choz 再次评论了地图所需的样式。我在上面包含了工作代码。

vad*_*lim 5

您可能忘记设置#map. 这是一个非常基本的示例,说明如何让它工作。

// create a map in the "map" div, set the view to a given place and zoom
var map = L.map("map").setView([39.50, -98.35], 5);

// add an OpenStreetMap tile layer
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map);
Run Code Online (Sandbox Code Playgroud)
#map {
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  top: 0;
  z-index: 0;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/leaflet.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/leaflet.js"></script>
<div id='map'></div>
Run Code Online (Sandbox Code Playgroud)