|
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 |
});
|