Geolocate.js
5.14 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for
* full list of contributors). Published under the Clear BSD license.
* See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */
/**
* @requires OpenLayers/Control.js
* @requires OpenLayers/Geometry/Point.js
* @requires OpenLayers/Projection.js
*/
/**
* Class: OpenLayers.Control.Geolocate
* The Geolocate control wraps w3c geolocation API into control that can be
* bound to a map, and generate events on location update
*
* To use this control requires to load the proj4js library if the projection
* of the map is not EPSG:4326 or EPSG:900913.
*
* Inherits from:
* - <OpenLayers.Control>
*/
OpenLayers.Control.Geolocate = OpenLayers.Class(OpenLayers.Control, {
/**
* Constant: EVENT_TYPES
* Supported event types:
* - *locationupdated* Triggered when browser return a new position
* - *locationfailed* Triggered when geolocation has failed
* - *locationuncapable* Triggered when control is activated on a browser
* which doesn't support geolocation
*/
EVENT_TYPES: ["locationupdated", "locationfailed", "locationuncapable"],
/**
* Property: geolocation
* {Object} The geolocation engine, as a property to be possibly mocked.
*/
geolocation: navigator.geolocation,
/**
* APIProperty: bind
* {Boolean} If true, map center will be set on location update.
*/
bind: true,
/**
* APIProperty: watch
* {Boolean} If true, position will be update regularly.
*/
watch: false,
/**
* APIProperty: geolocationOptions
* {Object} Options to pass to the navigator's geolocation API. See
* <http://dev.w3.org/geo/api/spec-source.html>. No specific
* option is passed to the geolocation API by default.
*/
geolocationOptions: null,
/**
* Constructor: OpenLayers.Control.Geolocate
* Create a new control to deal with browser geolocation API
*
*/
initialize: function(options) {
// concatenate events specific to this control with those from the base
this.EVENT_TYPES =
OpenLayers.Control.Geolocate.prototype.EVENT_TYPES.concat(
OpenLayers.Control.prototype.EVENT_TYPES
);
this.geolocationOptions = {};
OpenLayers.Control.prototype.initialize.apply(this, [options]);
},
/**
* Method: destroy
*/
destroy: function() {
this.deactivate();
OpenLayers.Control.prototype.destroy.apply(this, arguments);
},
/**
* Method: activate
* Activates the control.
*
* Returns:
* {Boolean} The control was effectively activated.
*/
activate: function () {
if (!this.geolocation) {
this.events.triggerEvent("locationuncapable");
return false;
}
if (OpenLayers.Control.prototype.activate.apply(this, arguments)) {
if (this.watch) {
this.watchId = this.geolocation.watchPosition(
OpenLayers.Function.bind(this.geolocate, this),
OpenLayers.Function.bind(this.failure, this),
this.geolocationOptions
);
} else {
this.getCurrentLocation();
}
return true;
}
return false;
},
/**
* Method: deactivate
* Deactivates the control.
*
* Returns:
* {Boolean} The control was effectively deactivated.
*/
deactivate: function () {
if (this.active && this.watchId !== null) {
this.geolocation.clearWatch(this.watchId);
}
return OpenLayers.Control.prototype.deactivate.apply(
this, arguments
);
},
/**
* Method: geolocate
* Activates the control.
*
*/
geolocate: function (position) {
var center = new OpenLayers.LonLat(
position.coords.longitude,
position.coords.latitude
).transform(
new OpenLayers.Projection("EPSG:4326"),
this.map.getProjectionObject()
);
if (this.bind) {
this.map.setCenter(center);
}
this.events.triggerEvent("locationupdated", {
position: position,
point: new OpenLayers.Geometry.Point(
center.lon, center.lat
)
});
},
/**
* APIMethod: getCurrentLocation
*
* Returns:
* {Boolean} Returns true if a event will be fired (successfull
* registration)
*/
getCurrentLocation: function() {
if (!this.active || this.watch) {
return false;
}
this.geolocation.getCurrentPosition(
OpenLayers.Function.bind(this.geolocate, this),
OpenLayers.Function.bind(this.failure, this),
this.geolocationOptions
);
return true;
},
/**
* Method: failure
* method called on browser's geolocation failure
*
*/
failure: function (error) {
this.events.triggerEvent("locationfailed", {error: error});
},
CLASS_NAME: "OpenLayers.Control.Geolocate"
});