Projet

Général

Profil

0001-misc-add-new-button-on-maps-to-re-ask-for-device-geo.patch

Frédéric Péters, 26 janvier 2017 11:27

Télécharger (5,76 ko)

Voir les différences:

Subject: [PATCH] misc: add new button on maps, to re-ask for device
 geolocation (#14478)

 wcs/qommon/http_response.py         |   1 +
 wcs/qommon/static/js/leaflet-gps.js | 128 ++++++++++++++++++++++++++++++++++++
 wcs/qommon/static/js/qommon.map.js  |   8 +++
 3 files changed, 137 insertions(+)
 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_javascript(['leaflet-gps.js'])
78 79
                    self.add_javascript(['../../i18n.js'])
79 80
                self.javascript_scripts.append(str(mapped_script_name))
80 81
                if script_name == 'afterjob.js':
wcs/qommon/static/js/leaflet-gps.js
1
/* adapted from https://github.com/stefanocudini/leaflet-gps
2
 *
3
 * Leaflet Control plugin for tracking gps position, with more options
4
 * by Stefano Cudini, stefano.cudini@gmail.com, http://labs.easyblog.it/
5
 * published under the MIT license.
6
 */
7

  
8
(function (factory) {
9
  if (typeof window.L === 'undefined')
10
    throw 'Leaflet must be loaded first';
11
  factory(window.L);
12
})(function (L) {
13

  
14
L.Control.Gps = L.Control.extend({
15
	includes: L.Mixin.Events,
16
	options: {
17
		style: {
18
			radius: 5,
19
			weight: 2,
20
			color: '#c20',
21
			opacity: 1,
22
			fillColor: '#f23',
23
			fillOpacity: 1
24
		},
25
		position: 'topleft'
26
	},
27

  
28
	initialize: function(options) {
29
		if(options && options.style)
30
			options.style = L.Util.extend({}, this.options.style, options.style);
31
		L.Util.setOptions(this, options);
32
		this._isActive = false; //global state of gps
33
		this._firstMoved = false; //global state of gps
34
		this._currentLocation = null; //store last location
35
	},
36

  
37
	onAdd: function (map) {
38
		this._map = map;
39

  
40
		var container = L.DomUtil.create('div', 'leaflet-control-gps leaflet-bar');
41

  
42
		this._button = L.DomUtil.create('a', 'gps-button', container);
43
		this._button.href = '#';
44
		this._button.text = '\uf192';
45
		this._button.style.fontFamily = 'FontAwesome';
46
		this._button.style.borderRadius = '4px';
47
		L.DomEvent
48
			.on(this._button, 'click', L.DomEvent.stop, this)
49
			.on(this._button, 'click', this._askGps, this);
50

  
51
		this._gpsMarker = new L.CircleMarker([0,0], this.options.style);
52

  
53
		this._map
54
			.on('locationfound', this._drawGps, this)
55
			.on('locationerror', this._errorGps, this);
56

  
57
		return container;
58
	},
59

  
60
	onRemove: function(map) {
61
		this.deactivate();
62
	},
63

  
64
	_askGps: function() {
65
		this.activate();
66
	},
67

  
68
	getLocation: function() {
69
		return this._currentLocation;
70
	},
71

  
72
	activate: function() {
73
		this._isActive = true;
74
		this._map.addLayer(this._gpsMarker);
75
		this._map.locate({
76
			enableHighAccuracy: true,
77
			watch: true,
78
			setView: false,
79
			maxZoom: null
80
		});
81
	},
82

  
83
	deactivate: function() {
84
		this._isActive = false;
85
		this._firstMoved = false;
86
		this._map.stopLocate();
87
		this._map.removeLayer( this._gpsMarker );
88
		this.fire('gps:disabled');
89
	},
90

  
91
	_drawGps: function(e) {
92
		this._currentLocation = e.latlng;
93

  
94
		this._gpsMarker.setLatLng(this._currentLocation);
95

  
96
		if(this._isActive && !this._firstMoved)
97
			this._moveTo(this._currentLocation);
98

  
99
                if (this._isActive) {
100
			this.fire('gps:located', {latlng: this._currentLocation, marker: this._gpsMarker});
101
                }
102
	},
103

  
104
	_moveTo: function(latlng) {
105
		this._firstMoved = true;
106
		this._map.panTo(latlng);
107
	},
108

  
109
	_errorGps: function(e) {
110
		this.deactivate();
111
	},
112

  
113
});
114

  
115
L.Map.addInitHook(function () {
116
	if (this.options.gpsControl) {
117
		this.gpsControl = L.control.gps(this.options.gpsControl);
118
		this.addControl(this.gpsControl);
119
	}
120
});
121

  
122
L.control.gps = function (options) {
123
	return new L.Control.Gps(options);
124
};
125

  
126
return L.Control.Gps;
127

  
128
});
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
     var gps_control = new L.Control.Gps();
17
     map.addControl(gps_control);
16 18
     var hidden = $(this).prev();
17 19
     map.marker = null;
18 20
     var latlng;
......
67 69
         else if (e.code == 2) message = WCS_I18N.geoloc_position_unavailable;
68 70
         else if (e.code == 3) message = WCS_I18N.geoloc_timeout;
69 71
         $map_widget.parent().parent().find('label').removeClass('activity');
72
         $map_widget.parent().parent().find('.geoloc-error').remove();
70 73
         $map_widget.parent().parent().find('label').after('<span class="geoloc-error">' + message + '</span>');
71 74
       });
75
       gps_control.on('gps:located', function(e) {
76
         map.panTo(e.latlng);
77
         map.stopLocate();
78
         $map_widget.trigger('set-geolocation', e.latlng);
79
       });
72 80
       $map_widget.parent().parent().find('label').addClass('activity')
73 81
       map.locate({timeout: 10000, maximumAge: 300000, enableHighAccuracy: false});
74 82
     }
75
-