javascript - geoJSON undefined 不是构造函数

标签 javascript jquery leaflet

我在 LeafLet 中总是收到错误“未定义不是构造函数”。 有人知道如何让这段代码工作吗?

jQuery(document).ready(function() {
    var map = L.map('canvas',{zoomControl: false}).setView([38.82259, -2.8125], 3);
    map.setMaxBounds([[84.67351256610522, -174.0234375], [-58.995311187950925, 223.2421875]]);
    var layer = new L.TileLayer("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", { noWrap: true}).addTo(map);

    var geojsonFeature = {
        "type": "Feature",
        "properties": {
            "name": "Coors Field",
            "amenity": "Baseball Stadium",
            "popupContent": "This is where the Rockies play!"
        },
        "geometry": {
            "type": "Point",
            "coordinates": [-104.99404, 39.75621]
        }
    };

    var geoJson = new L.geoJSON(geojsonFeature).addTo(map);

});

最佳答案

对我来说工作正常。验证您的 Leaflet css 和 JS 是否正确加载。请参阅Preparing your page

<html>

<head>
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css" integrity="sha512-07I2e+7D8p6he1SIM+1twR5TIrhUQn9+I6yjqD53JQjFiMf8EtC93ty0/5vJTZGF8aAocvHYNEDJajGdNx1IsQ=="
        crossorigin="" />
    <script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js" integrity="sha512-A7vV8IFfih/D732iSSKi20u/ooOfj/AGehOKq0f4vLT1Zr2Y+RX7C+w8A1gaSasGtRUZpF/NZgzSAu4/Gc41Lg=="
        crossorigin=""></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

</head>

<body>
    <div id="canvas" style="width: 100%; height: 100%;"></div>
    <script>
        jQuery(document).ready(function () {
            var map = L.map('canvas', { zoomControl: false }).setView([38.82259, -2.8125], 3);
            map.setMaxBounds([[84.67351256610522, -174.0234375], [-58.995311187950925, 223.2421875]]);
            var layer = new L.TileLayer("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", { noWrap: true }).addTo(map);

            var geojsonFeature = {
                "type": "Feature",
                "properties": {
                    "name": "Coors Field",
                    "amenity": "Baseball Stadium",
                    "popupContent": "This is where the Rockies play!"
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [-104.99404, 39.75621]
                }
            };

            var geoJson = new L.geoJSON(geojsonFeature).addTo(map);

        });
    </script>
</body>

</html>

关于javascript - geoJSON undefined 不是构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43875633/

相关文章:

javascript - 在AngularJs中,本地文件不显示img的ng-src

javascript - 单击显示内容并将鼠标悬停更新内容

javascript - jQuery 重定向到另一个文件

javascript - Leaflet、JS、示例代码不渲染 map

javascript - 为什么我在jade中找不到app.locals

javascript - 如何使用 Node 将图像上传到 S3

javascript - 通过 canvas.toDataURL 将 Canvas 保存到图像会导致黑色矩形

jquery - 使用 jQuery 更改背景图像

传单用户触发事件

javascript - 传单浏览器打印插件在打印时不显示比例