-
Notifications
You must be signed in to change notification settings - Fork 1
/
leaflet.autolocate.js
134 lines (108 loc) · 3.12 KB
/
leaflet.autolocate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
L.Control.Autolocate = L.Control.extend({
includes: L.Mixin.Events,
options: {
position: 'topleft',
maxZoom: 16,
showMarker: true,
showCircle: true,
button: {
title: "Get current location"
},
markerStyle: {
color: '#136AEC',
fillColor: '#2A93EE',
fillOpacity: 0.7,
weight: 2,
opacity: 0.9,
radius: 4
},
circleStyle: {
color: '#136AEC',
fillColor: '#2A93EE',
fillOpacity: 0.7,
weight: 2,
opacity: 0.9,
radius: 4
}
},
initialize: function(options) {
L.Util.setOptions(this, options);
},
onAdd: function (map) {
var className = 'leaflet-control-autolocate',
container = L.DomUtil.create('div', className);
this._map = map;
this._layer = new L.LayerGroup();
this._layer.addTo(this._map);
this._isActive = false;
this._locationData = null;
this._atLocation = false;
this._locateOptions = {
'setView': true,
'watch': false,
'maxZoom': this.options.maxZoom
};
this._createButton(this.options.button.title, 'leaflet-control-autolocate-btn', container, this.autolocate, this);
this._map.on('locationfound', this._onLocationFound, this);
this._map.on('locationerror', this._onLocationError, this);
this._map.on('move', this._locationChanged, this);
return container;
},
_onLocationFound: function (e) {
if (this._locationData &&
(this._locationData.latlng.lat != e.latlng.lat ||
this._locationData.latlng.lon != e.latlng.lon)) {
this._locationData._atLocation = false;
} else { this._atLocation = true; }
this._locationData = e;
//if (!self._isActive) {
// return;
//}
this._showCircle();
},
_onLocationError: function (err) {
alert(err.message);
},
_locationChanged: function (e) {
this._atLocation = false;
},
autolocate: function() {
if ( this._isActive && this._atLocation){
this._hideCircle();
} else {
if (!this._locationData || !this._atLocation) {
this._enterCurrentLocation();
} else { this._showCircle(); }
}
},
_hideCircle: function() {
if (this._circle) {
this._layer.removeLayer(this._circle);
this._isActive = false;
}
},
_showCircle: function() {
var radius = this._locationData.accuracy / 2;
this._layer.clearLayers();
if (this.options.showCircle) {
this._circle = L.circle(this._locationData.latlng, radius);
this._circle.addTo(this._layer);
}
L.circleMarker(this._locationData.latlng, this.options.markerStyle).addTo(this._layer);
this._isActive = true;
},
_enterCurrentLocation: function() {
this._map.locate(this._locateOptions);
},
_createButton: function (title, className, container, fn, context) {
var link = L.DomUtil.create('a', className, container);
link.href = '#';
link.title = title;
L.DomEvent
.on(link, 'click', L.DomEvent.stopPropagation)
.on(link, 'click', L.DomEvent.preventDefault)
.on(link, 'click', fn, context)
.on(link, 'dblclick', L.DomEvent.stopPropagation);
return link;
}
});