geoJSON undefined不是构造函数

时间:2017-05-09 16:51:33

标签: javascript jquery leaflet

我总是得到错误" undefined不是构造函数"在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);

});

1 个答案:

答案 0 :(得分:1)

适合我。验证您的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>