Leaflet:Uncaught TypeError:L.markerClusterGroup在尝试使用Cluster插件时不是函数

时间:2018-03-12 18:14:27

标签: javascript html leaflet

我正在尝试使用Leaflet的Cluster Group插件,但我一直得到" Uncaught TypeError:L.markerClusterGroup不是函数"

这是我简化的HTML文件:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Brewser</title>
        <link rel="stylesheet" type="text/css" href="brewser.css">
        <link rel="stylesheet" type="text/css" href="leaflet.css"/>
        <link href="https://fonts.googleapis.com/css?family=Lato:400,400i,700,700i,900,900i" rel="stylesheet">
        <link rel="stylesheet" type="text/css" href="dist/MarkerCluster.css"/>
        <link rel="stylesheet" type="text/css" href="dist/MarkerCluster.Default.css"/>      
    </head>
    <body>
        <script src="https://www.gstatic.com/firebasejs/4.9.0/firebase.js"></script>
        <script src="https://www.gstatic.com/firebasejs/4.9.0/firebase-app.js"></script>
        <script src="https://www.gstatic.com/firebasejs/4.9.0/firebase-database.js"></script>   
        <script src="leaflet.js" type="text/javascript"></script>
        <script src="app.js" type="text/javascript"></script>
        <script src="brewser.js" type="text/javascript"></script>
        <script src="dist/leaflet.markercluster-src.js"></script>
    </body>
</html>

这是我的JS:

var myMap = L.map('map').setView([49.197060, 16.611837], 13);
var markers = L.markerClusterGroup();

知道可能出现什么问题吗?

我正在使用的插件文件是dist目录中的文件:

leaflet.markercluster-src.js
leaflet.markercluster.js
leaflet.markercluster-src.js.map
leaflet.markercluster.js.map
MarkerCluster.css
MarkerCluster.Default.css

1 个答案:

答案 0 :(得分:1)

您的导入无序:

    <script src="https://www.gstatic.com/firebasejs/4.9.0/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/4.9.0/firebase-database.js"></script>   
    <script src="leaflet.js" type="text/javascript"></script>
    <script src="app.js" type="text/javascript"></script>
    <script src="brewser.js" type="text/javascript"></script>
    <script src="dist/leaflet.markercluster-src.js"></script>

应该是:

    <script src="https://www.gstatic.com/firebasejs/4.9.0/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/4.9.0/firebase-database.js"></script>   
    <script src="leaflet.js" type="text/javascript"></script>
    <!-- Notice where markercluster import is above app.js -->
    <script src="dist/leaflet.markercluster-src.js"></script>
    <script src="app.js" type="text/javascript"></script>
    <script src="brewser.js" type="text/javascript"></script>