Code coverage report for src/control/Control.Scale.js

Statements: 31.58% (12 / 38)      Branches: 10% (3 / 30)      Functions: 20% (2 / 10)      Lines: 31.58% (12 / 38)     

All files » src/control/ » Control.Scale.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113        1                   1   1       1   1 1   1               1 1   1 1                                                                                                                                               1      
/*
 * L.Control.Scale is used for displaying metric/imperial scale on the map.
 */
 
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;
 
		this._addScales(options, className, container);
 
		map.on(options.updateWhenIdle ? 'moveend' : 'move', this._update, this);
		map.whenReady(this._update, this);
 
		return container;
	},
 
	onRemove: function (map) {
		map.off(this.options.updateWhenIdle ? 'moveend' : 'move', this._update, this);
	},
 
	_addScales: function (options, className, container) {
		Eif (options.metric) {
			this._mScale = L.DomUtil.create('div', className + '-line', container);
		}
		Eif (options.imperial) {
			this._iScale = L.DomUtil.create('div', className + '-line', container);
		}
	},
 
	_update: function () {
		var bounds = this._map.getBounds(),
		    centerLat = bounds.getCenter().lat,
		    halfWorldMeters = 6378137 * Math.PI * Math.cos(centerLat * Math.PI / 180),
		    dist = halfWorldMeters * (bounds.getNorthEast().lng - bounds.getSouthWest().lng) / 180,
 
		    size = this._map.getSize(),
		    options = this.options,
		    maxMeters = 0;
 
		if (size.x > 0) {
			maxMeters = dist * (options.maxWidth / size.x);
		}
 
		this._updateScales(options, maxMeters);
	},
 
	_updateScales: function (options, maxMeters) {
		if (options.metric && maxMeters) {
			this._updateMetric(maxMeters);
		}
 
		if (options.imperial && maxMeters) {
			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;
 
		d = d >= 10 ? 10 : d >= 5 ? 5 : d >= 3 ? 3 : d >= 2 ? 2 : 1;
 
		return pow10 * d;
	}
});
 
L.control.scale = function (options) {
	return new L.Control.Scale(options);
};