Projet

Général

Profil

0001-manual-geoloc-stuff-draft-14478.patch

Frédéric Péters, 25 janvier 2017 17:18

Télécharger (12,5 ko)

Voir les différences:

Subject: [PATCH] manual geoloc stuff (draft, #14478)

 wcs/qommon/http_response.py           |   2 +
 wcs/qommon/static/css/leaflet-gps.css |  52 +++++++++
 wcs/qommon/static/images/gps-icon.png | Bin 0 -> 2081 bytes
 wcs/qommon/static/js/leaflet-gps.js   | 195 ++++++++++++++++++++++++++++++++++
 wcs/qommon/static/js/qommon.map.js    |   2 +
 5 files changed, 251 insertions(+)
 create mode 100644 wcs/qommon/static/css/leaflet-gps.css
 create mode 100644 wcs/qommon/static/images/gps-icon.png
 create mode 100644 wcs/qommon/static/js/leaflet-gps.js
wcs/qommon/http_response.py
75 75
                    self.add_javascript(['jquery.js'])
76 76
                    self.add_javascript(['../leaflet/leaflet.js'])
77 77
                    self.add_css_include('../leaflet/leaflet.css')
78
                    self.add_css_include('leaflet-gps.css')
79
                    self.add_javascript(['leaflet-gps.js'])
78 80
                    self.add_javascript(['../../i18n.js'])
79 81
                self.javascript_scripts.append(str(mapped_script_name))
80 82
                if script_name == 'afterjob.js':
wcs/qommon/static/css/leaflet-gps.css
1

  
2
.leaflet-container .leaflet-control-gps {
3
	position:relative;
4
	float:left;
5
	background:#fff;
6
	color:#1978cf;
7
	-moz-border-radius: 4px;
8
	-webkit-border-radius: 4px;
9
	border-radius: 4px;
10
	/*background-color: rgba(0, 0, 0, 0.25);*/
11
	background-color: rgba(255, 255, 255, 0.8);
12
	z-index:1000;	
13
	box-shadow: 0 1px 7px rgba(0,0,0,0.65);
14
	margin-left:10px;
15
	margin-top:10px;
16
}
17
.leaflet-control-gps .gps-button {
18
	display:block;
19
	float:left;
20
	width:26px;
21
	height:26px;
22
	background-image: url('../images/gps-icon.png');
23
	background-repeat: no-repeat;
24
	background-position: 1px 1px;
25
	background-color: #fff;
26
	border-radius:4px;
27
}
28
.leaflet-control-gps .gps-button:hover,
29
.leaflet-control-gps .gps-button.active:hover {
30
	background-color: #f4f4f4;
31
}
32
.leaflet-control-gps .gps-button.disabled {
33
	background-position: 1px -28px;
34
}
35
.leaflet-control-gps .gps-button.active {
36
	background-position: 1px -56px;
37
}
38

  
39
.leaflet-control-gps .gps-alert {
40
	position:absolute;
41
	left:26px;
42
	bottom:-1px;
43
	width:220px;
44
	padding:2px;
45
	line-height:.95em;
46
	color:#e00;
47
	border: 1px solid #888;	
48
	background-color: rgba(255, 255, 255, 0.75);
49
	border-radius:4px;
50
}
51

  
52

  
wcs/qommon/static/js/leaflet-gps.js
1
(function (factory) {
2
    if(typeof define === 'function' && define.amd) {
3
    //AMD
4
        define(['leaflet'], factory);
5
    } else if(typeof module !== 'undefined') {
6
    // Node/CommonJS
7
        module.exports = factory(require('leaflet'));
8
    } else {
9
    // Browser globals
10
        if(typeof window.L === 'undefined')
11
            throw 'Leaflet must be loaded first';
12
        factory(window.L);
13
    }
14
})(function (L) {
15

  
16
L.Control.Gps = L.Control.extend({
17

  
18
	includes: L.Mixin.Events,
19
	//
20
	//Managed Events:
21
	//	Event			Data passed			Description
22
	//
23
	//	gps:located		{latlng, marker}	fired after gps marker is located
24
	//	gps:disabled							fired after gps is disabled
25
	//
26
	//Methods exposed:
27
	//	Method 			Description
28
	//
29
	//  getLocation		return Latlng and marker of current position
30
	//  activate		active tracking on runtime
31
	//  deactivate		deactive tracking on runtime
32
	//
33
	options: {
34
		autoActive: false,		//activate control at startup
35
		autoCenter: false,		//move map when gps location change
36
		maxZoom: null,			//max zoom for autoCenter
37
		textErr: null,			//error message on alert notification
38
		callErr: null,			//function that run on gps error activating
39
		style: {				//default L.CircleMarker styles
40
			radius: 5,
41
			weight: 2,
42
			color: '#c20',
43
			opacity: 1,
44
			fillColor: '#f23',
45
			fillOpacity: 1
46
		},
47
		marker: null,			//L.Marker used for location, default use a L.CircleMarker
48
		accuracy: true,		//show accuracy Circle
49
		title: 'Center map on your location',
50
		position: 'topleft',
51
		transform: function(latlng) { return latlng },
52
		setView: false
53
		//TODO add gpsLayer
54
		//TODO timeout autoCenter
55
	},
56

  
57
	initialize: function(options) {
58
		if(options && options.style)
59
			options.style = L.Util.extend({}, this.options.style, options.style);
60
		L.Util.setOptions(this, options);
61
		this._errorFunc = this.options.callErr || this.showAlert;
62
		this._isActive = false;//global state of gps
63
		this._firstMoved = false;//global state of gps
64
		this._currentLocation = null;	//store last location
65
	},
66

  
67
	onAdd: function (map) {
68

  
69
		this._map = map;
70

  
71
		var container = L.DomUtil.create('div', 'leaflet-control-gps');
72

  
73
		this._button = L.DomUtil.create('a', 'gps-button', container);
74
		this._button.href = '#';
75
		this._button.title = this.options.title;
76
		L.DomEvent
77
			.on(this._button, 'click', L.DomEvent.stop, this)
78
			.on(this._button, 'click', this._switchGps, this);
79

  
80
		this._alert = L.DomUtil.create('div', 'gps-alert', container);
81
		this._alert.style.display = 'none';
82

  
83
		this._gpsMarker = this.options.marker ? this.options.marker : new L.CircleMarker([0,0], this.options.style);
84
		//if(this.options.accuracy)
85
		//	this._accuracyCircle = new L.Circle([0,0], this.options.style);
86

  
87
		this._map
88
			.on('locationfound', this._drawGps, this)
89
			.on('locationerror', this._errorGps, this);
90

  
91
		if(this.options.autoActive)
92
			this.activate();
93

  
94
		return container;
95
	},
96

  
97
	onRemove: function(map) {
98
		this.deactivate();
99
	},
100

  
101
	_switchGps: function() {
102
		if(this._isActive)
103
			this.deactivate();
104
		else
105
			this.activate();
106
	},
107

  
108
	getLocation: function() {	//get last location
109
		return this._currentLocation;
110
	},
111

  
112
	activate: function() {
113
		this._isActive = true;
114
		this._map.addLayer( this._gpsMarker );
115
		this._map.locate({
116
			enableHighAccuracy: true,
117
			watch: true,
118
			//maximumAge:s
119
			setView: this.options.setView,	//automatically sets the map view to the user location
120
			maxZoom: this.options.maxZoom
121
		});
122
	},
123

  
124
	deactivate: function() {
125
			this._isActive = false;
126
		this._firstMoved = false;
127
		this._map.stopLocate();
128
		L.DomUtil.removeClass(this._button, 'active');
129
		this._map.removeLayer( this._gpsMarker );
130
		//this._gpsMarker.setLatLng([-90,0]);  //move to antarctica!
131
		//TODO make method .hide() using _icon.style.display = 'none'
132
		this.fire('gps:disabled');
133
	},
134

  
135
	_drawGps: function(e) {
136
		//TODO use e.accuracy for gps circle radius/color
137
		this._currentLocation = this.options.transform(e.latlng);
138
			
139
		this._gpsMarker.setLatLng(this._currentLocation);
140

  
141
		if(this._isActive && (!this._firstMoved || this.options.autoCenter))
142
			this._moveTo(this._currentLocation);
143
	//    	if(this._gpsMarker.accuracyCircle)
144
	//    		this._gpsMarker.accuracyCircle.setRadius((e.accuracy / 2).toFixed(0));
145
			
146
		this.fire('gps:located', {latlng: this._currentLocation, marker: this._gpsMarker});
147
		
148
		L.DomUtil.addClass(this._button, 'active');
149
	},
150

  
151
	_moveTo: function(latlng) {
152
		this._firstMoved = true;
153
		if(this.options.maxZoom)
154
			this._map.setView(latlng, Math.min(this._map.getZoom(), this.options.maxZoom) );
155
		else
156
			this._map.panTo(latlng);
157
	},
158

  
159
	_errorGps: function(e) {
160
		this.deactivate();
161
		//this._errorFunc.call(this, this.options.textErr || e.message);
162
	},
163

  
164
	/*	_updateAccuracy: function (event) {
165
			var newZoom = this._map.getZoom(),
166
				scale = this._map.options.crs.scale(newZoom);
167
			this._gpsMarker.setRadius(this.options.style.radius * scale);
168
			this._gpsMarker.redraw();
169
		},
170
	*/
171
	showAlert: function(text) {
172
		this._alert.style.display = 'block';
173
		this._alert.innerHTML = text;
174
		var that = this;
175
		clearTimeout(this.timerAlert);
176
		this.timerAlert = setTimeout(function() {
177
			that._alert.style.display = 'none';
178
		}, 5000);
179
	}
180
});
181

  
182
L.Map.addInitHook(function () {
183
	if (this.options.gpsControl) {
184
		this.gpsControl = L.control.gps(this.options.gpsControl);
185
		this.addControl(this.gpsControl);
186
	}
187
});
188

  
189
L.control.gps = function (options) {
190
	return new L.Control.Gps(options);
191
};
192

  
193
return L.Control.Gps;
194

  
195
});
wcs/qommon/static/js/qommon.map.js
13 13
     var min_zoom = parseInt($map_widget.data('min_zoom'));
14 14
     if (! isNaN(min_zoom)) map_options.minZoom = min_zoom;
15 15
     var map = L.map($(this).attr('id'), map_options);
16
     map.addControl( new L.Control.Gps() );
16 17
     var hidden = $(this).prev();
17 18
     map.marker = null;
18 19
     var latlng;
......
67 68
         else if (e.code == 2) message = WCS_I18N.geoloc_position_unavailable;
68 69
         else if (e.code == 3) message = WCS_I18N.geoloc_timeout;
69 70
         $map_widget.parent().parent().find('label').removeClass('activity');
71
         $map_widget.parent().parent().find('.geoloc-error').remove();
70 72
         $map_widget.parent().parent().find('label').after('<span class="geoloc-error">' + message + '</span>');
71 73
       });
72 74
       $map_widget.parent().parent().find('label').addClass('activity')
73
-