问题
I'm trying to use leaflet markercluster. but when I run it, gives me an error as:
Uncaught TypeError: Cannot read property 'addLayer' of undefined
my code in functions.js is:
for (var i in ODO) {
var latlng = L.latLng({ lat: ODO[i].LastX, lng: ODO[i].LastY });
L.marker( latlng ).addTo(map);
}
var markerClusters = new L.MarkerClusterGroup().addTo(map);
for ( var i = 0; i < ODO.length ; i++ )
{
var popup = ODO[i].branchcode +
'<br/><b>دوره:</b> ' + ODO[i].saleprd +
'<br/><b>سال :</b> ' + ODO[i].saleyear;
var map = L.marker( [ODO[i].LastX, ODO[i].LastY] )
.bindPopup( popup );
markerClusters.addLayer( map );
}
map.addLayer( markerClusters );
markerClusterLayer = L.markerClusterGroup({
disableClusteringAtZoom: 13
}).addTo(map);
and the order of calling cdn is:
<!---leaflet css -->
<link rel="stylesheet" href="css/leaflet.css" integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A=="
crossorigin=""/>
<!---marker cluster css -->
<link rel="stylesheet" href="css/MarkerCluster.css" />
<link rel="stylesheet" href="css/MarkerCluster.Default.css" />
<script src="script/config.js"></script>
<!-- Load Leaflet from CDN -->
<script src="script/leaflet.js" integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
crossorigin=""></script>
<script src="script/jquery-3.4.1.min.js"></script>
<!-- marker Cluster CDN -->
<script src="script/leaflet.markercluster.js"></script>
<!-- Load Esri Leaflet from CDN -->
<script src="script/esri-leaflet.js"></script>
<!--- amCharts CDN -->
<script src="script/core.js"></script>
<script src="script/charts.js"></script>
<script src="script/maps.js"></script>
<script src="script/animated.js"></script>
also, I have the leaflet layer code in my app.js like this:
function Init_Map()
{
map = L.map('map').setView( [36.564012,53.060300], 8);
// add the OpenStreetMap tiles
L.tileLayer('http://mt0.google.com/vt/lyrs=p&hl=en&x={x}&y={y}&z={z}&s=Ga', {
maxZoom: 19,
attribution: '© <a href="https://openstreetmap.org/copyright">OpenStreetMap contributors</a>'
}).addTo(map);
}
does anyone know the reason for this error?
回答1:
I called the Init_Map();
function in the last part of my markerclustering function and it just worked
来源:https://stackoverflow.com/questions/64822586/uncaught-typeerror-cannot-read-property-addlayer-of-undefined