如何在传单中加载geoJSON?

How to load geoJSON in leaflet?

我正在使用 Leaflet 在地图上工作,我有兴趣绘制一些连接我创建的国家标记(6 个正在工作的标记)的多段线。唯一的问题是,当我 运行 代码时,geojson 没有加载到地图上,即使我已经检查过代码没有任何问题并且没有错误弹出窗口。以下是从头到尾的代码:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css"
        integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
        crossorigin=""/>
        <!-- Make sure you put this AFTER Leaflet's CSS -->
        <script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js"
        integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew=="
        crossorigin=""></script>
        <style>
            * {
                margin: 0;
                padding: 0;
            }
            #map {
                height: 550px;
                width: 100%;
            }
        </style>
        <title>Leaflet JS map testing</title>
    </head>
    <body>
        <h2>Leaflet JS Map Testing</h2>
        <div id="map"></div>
        <script>
            var mymap = L.map('map').setView([20.594, 78.962], 4); //L.map('map') because it follows the div id in the parentheses
            const attribution = 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>'
            const tileUrl = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
            const tiles = L.tileLayer(tileUrl, { attribution });
            tiles.addTo(mymap);

            // Array of marker coordinates
            var markers = [
                {
                    coords:[4.21, 101.97],
                    country:'Malaysia',
                },
                {
                    coords:[20.594, 78.962],
                    country:'India',
                },
                {
                    coords:[35.861, 104.195],
                    country:'China',
                },
                {
                    coords:[23.421, 53.8478],
                    country:'UAE',
                },
                {
                    coords:[23.6978, 120.9605],
                    country:'Taiwan',
                },
                {
                    coords:[0.7892, 113.9213],
                    country:'Indonesia',
                },
            ];
            // Loop through markers
            for(var i = 0; i<markers.length; i++){
                addMarker(markers[i]);
            }
            // To add the marker coordinates
            function addMarker(props){ // Will not set the marker for the countries
                var marker = L.marker(props.coords).addTo(mymap).bindPopup(props.country).openPopup();
            }
            addMarker(); // To call the function

            // For GeoJSON features
            var myLines = {
                "type": "Feature",
                "properties": {
                    "name": "Please work",
                },
                "geometry": {
                    "type": "LineString",
                    "coordinates": [
                        [113.9213, 0.7892],[53.8478, 23.421],[101.97, 4.21]
                        ]
                }
            };
            L.geoJSON(myLines).addTo(map);
        </script>
    </body>
</html>

如果您知道 geojson 在我的地图上不起作用的原因或者代码是否有任何问题,请告诉我。

不应该(至少)在最后一行 L.geoJSON(myLines).addTo(mymap); 吗?