added scale control

This commit is contained in:
mourner 2012-02-25 17:04:27 +02:00
parent 79eb5bcb32
commit 90198ec519
8 changed files with 249 additions and 20 deletions

View File

@ -11,6 +11,7 @@ An in-progress version being developed on the master branch.
* Added configurable **panning inertia** - after a quick pan, the map slows down in the same direction.
* Added **polyline and polygon editing**. [#174](https://github.com/CloudMade/Leaflet/issues/174)
* Adde an unobtrusive **scale control**.
* Added `DivIcon` class that easily allows you to create lightweight div-based markers.
* Added `Rectangle` vector layer (by [@JasonSanford](https://github.com/JasonSanford)). [#504](https://github.com/CloudMade/Leaflet/pull/504)
@ -65,14 +66,14 @@ An in-progress version being developed on the master branch.
* Fixed a bug where `TileLayer` `setOpacity` wouldn't work when setting it back to 1.
* Fixed a bug where vector layer `setStyle({stroke: false})` wouldn't remove stroke and the same for fill. [#441](https://github.com/CloudMade/Leaflet/issues/441)
* Fixed a bug where `Marker` `bindPopup` method wouldn't take `offset` option into account.
#### Browser bugfixes
* Fixed inability to use scrolled content inside popup due to mouse wheel propagation.
* Fixed a bug that caused jumping/stuttering of panning animation in some cases.
* Fixed a bug where popup size was calculated incorrectly in IE.
* Fixed a bug where cursor would flicker when dragging a marker.
#### Mobile browser bugfixes
* Fixed a bug with false map click events on pinch-zoom and zoom/layers controls click. [#485](https://github.com/CloudMade/Leaflet/issues/485)

View File

@ -114,12 +114,12 @@ var deps = {
src: ['layer/vector/MultiPoly.js'],
deps: ['FeatureGroup', 'Polyline', 'Polygon'],
desc: 'MultiPolygon and MultyPolyline layers.'
},
Rectangle: {
src: ['layer/vector/Rectangle.js'],
deps: ['Polygon'],
desc: ['Rectangle overlays.']
},
Rectangle: {
src: ['layer/vector/Rectangle.js'],
deps: ['Polygon'],
desc: ['Rectangle overlays.']
},
Circle: {
@ -208,6 +208,13 @@ var deps = {
desc: 'Attribution control.'
},
ControlScale: {
src: ['control/Control.js',
'map/ext/Map.Control.js',
'control/Control.Scale.js'],
desc: 'Scale control.'
},
ControlLayers: {
src: ['control/Control.js',
'map/ext/Map.Control.js',

View File

@ -43,6 +43,8 @@
map.addControl(layersControl);
map.addControl(new L.Control.Scale());
</script>
</body>
</html>

View File

@ -89,6 +89,7 @@
'control/Control.Zoom.js',
'control/Control.Attribution.js',
'control/Control.Layers.js',
'control/Control.Scale.js'
];
function getSrcUrl() {

99
dist/leaflet-src.js vendored
View File

@ -5506,6 +5506,103 @@ L.Control.Attribution = L.Control.extend({
});
L.Control.Scale = L.Control.extend({
options: {
position: 'bottomleft',
maxWidth: 100,
metric: true,
imperial: true,
updateWhenIdle: false
},
onAdd: function (map) {
this._map = map;
var className = 'leaflet-control-scale',
container = L.DomUtil.create('div', className),
options = this.options;
if (options.metric) {
this._mScale = L.DomUtil.create('div', className + '-line', container);
}
if (options.imperial) {
this._iScale = L.DomUtil.create('div', className + '-line', container);
}
map.on(options.updateWhenIdle ? 'moveend' : 'move', this._update, this);
this._update();
return container;
},
onRemove: function (map) {
map.off(this.options.updateWhenIdle ? 'moveend' : 'move', this._update, this);
},
_update: function () {
var bounds = this._map.getBounds(),
centerLat = bounds.getCenter().lat,
left = new L.LatLng(centerLat, bounds.getSouthWest().lng),
right = new L.LatLng(centerLat, bounds.getNorthEast().lng),
size = this._map.getSize(),
options = this.options,
maxMeters = left.distanceTo(right) * (options.maxWidth / size.x);
if (options.metric) {
this._updateMetric(maxMeters);
}
if (options.imperial) {
this._updateImperial(maxMeters);
}
},
_updateMetric: function (maxMeters) {
var meters = this._getRoundNum(maxMeters);
this._mScale.style.width = this._getScaleWidth(meters / maxMeters) + 'px';
this._mScale.innerHTML = meters < 1000 ? meters + ' m' : (meters / 1000) + ' km';
},
_updateImperial: function (maxMeters) {
var maxFeet = maxMeters * 3.2808399,
scale = this._iScale,
maxMiles, miles, feet;
if (maxFeet > 5280) {
maxMiles = maxFeet / 5280;
miles = this._getRoundNum(maxMiles);
scale.style.width = this._getScaleWidth(miles / maxMiles) + 'px';
scale.innerHTML = miles + ' mi';
} else {
feet = this._getRoundNum(maxFeet);
scale.style.width = this._getScaleWidth(feet / maxFeet) + 'px';
scale.innerHTML = feet + ' ft';
}
},
_getScaleWidth: function (ratio) {
return Math.round(this.options.maxWidth * ratio) - 10;
},
_getRoundNum: function (num) {
var pow10 = Math.pow(10, (Math.floor(num) + '').length - 1),
d = num / pow10;
return pow10 * (d >= 10 ?
10 :
d >= 5 ?
5 :
d >= 2 ? 2 : 1);
}
});
L.Control.Layers = L.Control.extend({
options: {
@ -5981,7 +6078,7 @@ L.Map.include(!(L.Transition && L.Transition.implemented()) ? {} : {
if (!this._panTransition) {
this._panTransition = new L.Transition(this._mapPane);
//this._panTransition.on('step', this._onPanTransitionStep, this);
this._panTransition.on('step', this._onPanTransitionStep, this);
this._panTransition.on('end', this._onPanTransitionEnd, this);
}

45
dist/leaflet.css vendored
View File

@ -162,7 +162,7 @@ a.leaflet-active {
}
.leaflet-control-layers {
box-shadow: 0 0 7px #999;
box-shadow: 0 1px 7px #999;
background: #f8f8f9;
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
@ -206,23 +206,48 @@ a.leaflet-active {
}
.leaflet-container .leaflet-control-attribution {
margin: 0;
padding: 0 5px;
font: 11px/1.5 "Helvetica Neue", Arial, Helvetica, sans-serif;
color: #333;
background-color: rgba(255, 255, 255, 0.7);
box-shadow: 0 0 5px #bbb;
margin: 0;
}
.leaflet-control-attribution,
.leaflet-control-scale-line {
padding: 0 5px;
color: #333;
}
.leaflet-container .leaflet-control-attribution,
.leaflet-container .leaflet-control-scale {
font: 11px/1.5 "Helvetica Neue", Arial, Helvetica, sans-serif;
}
.leaflet-left .leaflet-control-scale {
margin-left: 5px;
}
.leaflet-bottom .leaflet-control-scale {
margin-bottom: 5px;
}
.leaflet-control-scale-line {
border: 2px solid #777;
border-top: none;
color: black;
text-shadow: 1px 1px 1px #fff;
background-color: rgba(255, 255, 255, 0.5);
}
.leaflet-control-scale-line:nth-child(2) {
border-top: 2px solid #777;
border-bottom: none;
margin-top: -2px;
}
.leaflet-touch .leaflet-control-attribution, .leaflet-touch .leaflet-control-layers {
box-shadow: none;
}
}
.leaflet-touch .leaflet-control-layers {
border: 5px solid #bbb;
}
}
/* Fade animations */

2
dist/leaflet.js vendored

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,96 @@
L.Control.Scale = L.Control.extend({
options: {
position: 'bottomleft',
maxWidth: 100,
metric: true,
imperial: true,
updateWhenIdle: false
},
onAdd: function (map) {
this._map = map;
var className = 'leaflet-control-scale',
container = L.DomUtil.create('div', className),
options = this.options;
if (options.metric) {
this._mScale = L.DomUtil.create('div', className + '-line', container);
}
if (options.imperial) {
this._iScale = L.DomUtil.create('div', className + '-line', container);
}
map.on(options.updateWhenIdle ? 'moveend' : 'move', this._update, this);
this._update();
return container;
},
onRemove: function (map) {
map.off(this.options.updateWhenIdle ? 'moveend' : 'move', this._update, this);
},
_update: function () {
var bounds = this._map.getBounds(),
centerLat = bounds.getCenter().lat,
left = new L.LatLng(centerLat, bounds.getSouthWest().lng),
right = new L.LatLng(centerLat, bounds.getNorthEast().lng),
size = this._map.getSize(),
options = this.options,
maxMeters = left.distanceTo(right) * (options.maxWidth / size.x);
if (options.metric) {
this._updateMetric(maxMeters);
}
if (options.imperial) {
this._updateImperial(maxMeters);
}
},
_updateMetric: function (maxMeters) {
var meters = this._getRoundNum(maxMeters);
this._mScale.style.width = this._getScaleWidth(meters / maxMeters) + 'px';
this._mScale.innerHTML = meters < 1000 ? meters + ' m' : (meters / 1000) + ' km';
},
_updateImperial: function (maxMeters) {
var maxFeet = maxMeters * 3.2808399,
scale = this._iScale,
maxMiles, miles, feet;
if (maxFeet > 5280) {
maxMiles = maxFeet / 5280;
miles = this._getRoundNum(maxMiles);
scale.style.width = this._getScaleWidth(miles / maxMiles) + 'px';
scale.innerHTML = miles + ' mi';
} else {
feet = this._getRoundNum(maxFeet);
scale.style.width = this._getScaleWidth(feet / maxFeet) + 'px';
scale.innerHTML = feet + ' ft';
}
},
_getScaleWidth: function (ratio) {
return Math.round(this.options.maxWidth * ratio) - 10;
},
_getRoundNum: function (num) {
var pow10 = Math.pow(10, (Math.floor(num) + '').length - 1),
d = num / pow10;
return pow10 * (d >= 10 ?
10 :
d >= 5 ?
5 :
d >= 2 ? 2 : 1);
}
});