Subversion Repositories eFlore/Applications.moissonnage

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 delphine 1
/*
2
 * L.TileLayer is used for standard xyz-numbered tile layers.
3
 */
4
//(function (ymaps, L) {
5
 
6
L.Yandex = L.Class.extend({
7
	includes: L.Mixin.Events,
8
 
9
	options: {
10
		minZoom: 0,
11
		maxZoom: 18,
12
		attribution: '',
13
		opacity: 1,
14
		traffic: false
15
	},
16
 
17
	// Possible types: map, satellite, hybrid, publicMap, publicMapHybrid
18
	initialize: function(type, options) {
19
		L.Util.setOptions(this, options);
20
 
21
		this._type = "yandex#" + (type || 'map');
22
	},
23
 
24
	onAdd: function(map, insertAtTheBottom) {
25
		this._map = map;
26
		this._insertAtTheBottom = insertAtTheBottom;
27
 
28
		// create a container div for tiles
29
		this._initContainer();
30
		this._initMapObject();
31
 
32
		// set up events
33
		map.on('viewreset', this._resetCallback, this);
34
 
35
		this._limitedUpdate = L.Util.limitExecByInterval(this._update, 150, this);
36
		map.on('move', this._update, this);
37
 
38
		map._controlCorners['bottomright'].style.marginBottom = "3em";
39
 
40
		this._reset();
41
		this._update(true);
42
	},
43
 
44
	onRemove: function(map) {
45
		this._map._container.removeChild(this._container);
46
 
47
		this._map.off('viewreset', this._resetCallback, this);
48
 
49
		this._map.off('move', this._update, this);
50
 
51
		map._controlCorners['bottomright'].style.marginBottom = "0em";
52
	},
53
 
54
	getAttribution: function() {
55
		return this.options.attribution;
56
	},
57
 
58
	setOpacity: function(opacity) {
59
		this.options.opacity = opacity;
60
		if (opacity < 1) {
61
			L.DomUtil.setOpacity(this._container, opacity);
62
		}
63
	},
64
 
65
	setElementSize: function(e, size) {
66
		e.style.width = size.x + "px";
67
		e.style.height = size.y + "px";
68
	},
69
 
70
	_initContainer: function() {
71
		var tilePane = this._map._container,
72
			first = tilePane.firstChild;
73
 
74
		if (!this._container) {
75
			this._container = L.DomUtil.create('div', 'leaflet-yandex-layer leaflet-top leaflet-left');
76
			this._container.id = "_YMapContainer_" + L.Util.stamp(this);
77
			this._container.style.zIndex = "auto";
78
		}
79
 
80
		if (this.options.overlay) {
81
			first = this._map._container.getElementsByClassName('leaflet-map-pane')[0];
82
			first = first.nextSibling;
83
			// XXX: Bug with layer order
84
			if (L.Browser.opera)
85
				this._container.className += " leaflet-objects-pane";
86
		}
87
		tilePane.insertBefore(this._container, first);
88
 
89
		this.setOpacity(this.options.opacity);
90
		this.setElementSize(this._container, this._map.getSize());
91
	},
92
 
93
	_initMapObject: function() {
94
		if (this._yandex) return;
95
 
96
		// Check that ymaps.Map is ready
97
		if (ymaps.Map === undefined) {
98
			console.debug("L.Yandex: Waiting on ymaps.load('package.map')");
99
			return ymaps.load(["package.map"], this._initMapObject, this);
100
		}
101
 
102
		// If traffic layer is requested check if control.TrafficControl is ready
103
		if (this.options.traffic)
104
			if (ymaps.control === undefined ||
105
			    ymaps.control.TrafficControl === undefined) {
106
				console.debug("L.Yandex: loading traffic and controls");
107
				return ymaps.load(["package.traffic", "package.controls"],
108
					this._initMapObject, this);
109
			}
110
 
111
		var map = new ymaps.Map(this._container, {center: [0,0], zoom: 0, behaviors: []});
112
 
113
		if (this.options.traffic)
114
			map.controls.add(new ymaps.control.TrafficControl({shown: true}));
115
 
116
		if (this._type == "yandex#null") {
117
			this._type = new ymaps.MapType("null", []);
118
			map.container.getElement().style.background = "transparent";
119
		}
120
		map.setType(this._type)
121
 
122
		this._yandex = map;
123
		this._update(true);
124
	},
125
 
126
	_resetCallback: function(e) {
127
		this._reset(e.hard);
128
	},
129
 
130
	_reset: function(clearOldContainer) {
131
		this._initContainer();
132
	},
133
 
134
	_update: function(force) {
135
		if (!this._yandex) return;
136
		this._resize(force);
137
 
138
		var center = this._map.getCenter();
139
		var _center = [center.lat, center.lng];
140
		var zoom = this._map.getZoom();
141
 
142
		if (force || this._yandex.getZoom() != zoom)
143
			this._yandex.setZoom(zoom);
144
		this._yandex.panTo(_center, {duration: 0, delay: 0});
145
	},
146
 
147
	_resize: function(force) {
148
		var size = this._map.getSize(), style = this._container.style;
149
		if (style.width == size.x + "px" &&
150
		    style.height == size.y + "px")
151
			if (force != true) return;
152
		this.setElementSize(this._container, size);
153
		var b = this._map.getBounds(), sw = b.getSouthWest(), ne = b.getNorthEast();
154
		this._yandex.container.fitToViewport();
155
	}
156
});
157
//})(ymaps, L)