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