This commit is contained in:
danzel
2012-07-23 15:44:26 +12:00
parent 6e81c385e1
commit 45d9f8f841
2 changed files with 156 additions and 0 deletions
+68
View File
@@ -0,0 +1,68 @@
<!DOCTYPE html>
<html>
<head>
<title>Leaflet debug page</title>
<link rel="stylesheet" href="../lib/leaflet-dist/leaflet.css" />
<!--[if lte IE 8]><link rel="stylesheet" href="../lib/leaflet-dist/leaflet.ie.css" /><![endif]-->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="screen.css" />
<script src="../lib/leaflet-dist/leaflet-src.js"></script>
<link rel="stylesheet" href="../src/MarkerCluster.css" />
<link rel="stylesheet" href="../src/MarkerCluster.Default.css" />
<script src="../src/MarkerClusterGroup.js"></script>
<script src="../src/MarkerCluster.js"></script>
<script src="../src/MarkerCluster.Spiderfier.js"></script>
</head>
<body>
<div id="map"></div>
<button id="populate">Populate 1 marker</button>
<button id="remove">Remove 1 marker</button>
<script type="text/javascript">
var cloudmadeUrl = 'http://{s}.tile.cloudmade.com/BC9A493B41014CAABB98F0471D759707/997/256/{z}/{x}/{y}.png',
cloudmadeAttribution = 'Map data &copy; 2011 OpenStreetMap contributors, Imagery &copy; 2011 CloudMade',
cloudmade = new L.TileLayer(cloudmadeUrl, {maxZoom: 18, attribution: cloudmadeAttribution}),
latlng = new L.LatLng(50.5, 30.51);
var map = new L.Map('map', {center: latlng, zoom: 15, layers: [cloudmade]});
var markers = new L.MarkerClusterGroup();
function populate() {
for (var i = 0; i < 100; i++) {
var m = new L.Marker(getRandomLatLng(map));
markers.addLayer(m);
}
return false;
}
function getRandomLatLng(map) {
var bounds = map.getBounds(),
southWest = bounds.getSouthWest(),
northEast = bounds.getNorthEast(),
lngSpan = northEast.lng - southWest.lng,
latSpan = northEast.lat - southWest.lat;
return new L.LatLng(
southWest.lat + latSpan * Math.random(),
southWest.lng + lngSpan * Math.random());
}
markers.on('click', function (a) {
if (a.layer instanceof L.MarkerCluster) {
console.log('cluster ' + a.layer.getAllChildMarkers().length);
a.layer.spiderfy();
} else {
console.log('marker ' + a.layer);
}
});
populate();
map.addLayer(markers);
</script>
</body>
</html>
+88
View File
@@ -0,0 +1,88 @@
//This code is 100% based on https://github.com/jawj/OverlappingMarkerSpiderfier-Leaflet
//Huge thanks to jawj for implementing it first to make my job easy :-)
L.MarkerCluster.include({
_2PI: Math.PI * 2,
_circleFootSeparation: 25, //related to circumference of circle
_circleStartAngle: Math.PI / 6,
_spiralFootSeparation: 28, //related to size of spiral (experiment!)
_spiralLengthStart: 11,
_spiralLengthFactor: 5,
_circleSpiralSwitchover: 9, //show spiral instead of circle from this marker count upwards.
// 0 -> always spiral; Infinity -> always circle
spiderfy: function () {
var childMarkers = this.getAllChildMarkers(),
group = this._group,
map = group._map,
center = map.latLngToLayerPoint(this._latlng),
markerOffsets,
i, m;
//TODO Maybe: childMarkers order by distance to center
if (childMarkers.length >= this._circleSpiralSwitchover) {
markerOffsets = this._generatePointsSpiral(childMarkers.length, center);
} else {
center.y += 10; //Otherwise circles look wrong
markerOffsets = this._generatePointsCircle(childMarkers.length, center);
}
for (i = childMarkers.length - 1; i >= 0; i--) {
m = childMarkers[i];
m._backupPosSpider = m._latlng;
m.setLatLng(map.layerPointToLatLng(markerOffsets[i]));
m.setZIndexOffset(1000000); //Make these appear on top of EVERYTHING
L.FeatureGroup.prototype.addLayer.call(group, m);
var leg = new L.Polyline([this._latlng, m._latlng], { weight: 1.5, color: '#222' });
map.addLayer(leg);
}
this.setOpacity(0.3);
},
unspiderfy: function () {
if (false) {
return;
}
//TODO
},
_generatePointsCircle: function (count, centerPt) {
var circumference = this._circleFootSeparation * (2 + count),
legLength = circumference / this._2PI, //radius from circumference
angleStep = this._2PI / count,
res = [],
i, angle;
res.length = count;
for (i = count - 1; i >= 0; i--) {
angle = this._circleStartAngle + i * angleStep;
res[i] = new L.Point(centerPt.x + legLength * Math.cos(angle), centerPt.y + legLength * Math.sin(angle));
}
return res;
},
_generatePointsSpiral: function (count, centerPt) {
var legLength = this._spiralLengthStart,
angle = 0,
res = [],
i;
res.length = count;
for (i = count - 1; i >= 0; i--) {
angle += this._spiralFootSeparation / legLength + i * 0.0005;
res[i] = new L.Point(centerPt.x + legLength * Math.cos(angle), centerPt.y + legLength * Math.sin(angle));
legLength += this._2PI * this._spiralLengthFactor / angle;
}
return res;
}
});