Project

General

Profile

Download (5.6 KB) Statistics
| Branch: | Tag: | Revision:
1
/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
2
 * full list of contributors). Published under the 2-clause BSD license.
3
 * See license.txt in the OpenLayers distribution or repository for the
4
 * full text of the license. */
5

    
6
/**
7
 * @requires OpenLayers/Control.js
8
 * @requires OpenLayers/Geometry/Point.js
9
 * @requires OpenLayers/Projection.js
10
 */
11

    
12
/**
13
 * Class: OpenLayers.Control.Geolocate
14
 * The Geolocate control wraps w3c geolocation API into control that can be
15
 * bound to a map, and generate events on location update
16
 *
17
 * To use this control requires to load the proj4js library if the projection
18
 * of the map is not EPSG:4326 or EPSG:900913.
19
 *
20
 * Inherits from:
21
 *  - <OpenLayers.Control>
22
 */
23
OpenLayers.Control.Geolocate = OpenLayers.Class(OpenLayers.Control, {
24

    
25
    /** 
26
     * APIProperty: events
27
     * {<OpenLayers.Events>} Events instance for listeners and triggering
28
     *     control specific events.
29
     *
30
     * Register a listener for a particular event with the following syntax:
31
     * (code)
32
     * control.events.register(type, obj, listener);
33
     * (end)
34
     *
35
     * Supported event types (in addition to those from <OpenLayers.Control.events>):
36
     * locationupdated - Triggered when browser return a new position. Listeners will 
37
     *     receive an object with a 'position' property which is the browser.geolocation.position
38
     *     native object, as well as a 'point' property which is the location transformed in the 
39
     *     current map projection.
40
     * locationfailed - Triggered when geolocation has failed
41
     * locationuncapable - Triggered when control is activated on a browser
42
     *     which doesn't support geolocation
43
     */
44

    
45
    /**
46
     * Property: geolocation
47
     * {Object} The geolocation engine, as a property to be possibly mocked.
48
     * This is set lazily to avoid a memory leak in IE9.
49
     */
50
    geolocation: null,
51

    
52
    /**
53
     * Property: available
54
     * {Boolean} The navigator.geolocation object is available.
55
     */
56
    available: ('geolocation' in navigator),
57

    
58
    /**
59
     * APIProperty: bind
60
     * {Boolean} If true, map center will be set on location update.
61
     */
62
    bind: true,
63

    
64
    /**
65
     * APIProperty: watch
66
     * {Boolean} If true, position will be update regularly.
67
     */
68
    watch: false,
69

    
70
    /**
71
     * APIProperty: geolocationOptions
72
     * {Object} Options to pass to the navigator's geolocation API. See
73
     *     <http://dev.w3.org/geo/api/spec-source.html>. No specific
74
     *     option is passed to the geolocation API by default.
75
     */
76
    geolocationOptions: null,
77

    
78
    /**
79
     * Constructor: OpenLayers.Control.Geolocate
80
     * Create a new control to deal with browser geolocation API
81
     *
82
     */
83

    
84
    /**
85
     * Method: destroy
86
     */
87
    destroy: function() {
88
        this.deactivate();
89
        OpenLayers.Control.prototype.destroy.apply(this, arguments);
90
    },
91

    
92
    /**
93
     * Method: activate
94
     * Activates the control.
95
     *
96
     * Returns:
97
     * {Boolean} The control was effectively activated.
98
     */
99
    activate: function () {
100
        if (this.available && !this.geolocation) {
101
            // set lazily to avoid IE9 memory leak
102
            this.geolocation = navigator.geolocation;
103
        }
104
        if (!this.geolocation) {
105
            this.events.triggerEvent("locationuncapable");
106
            return false;
107
        }
108
        if (OpenLayers.Control.prototype.activate.apply(this, arguments)) {
109
            if (this.watch) {
110
                this.watchId = this.geolocation.watchPosition(
111
                    OpenLayers.Function.bind(this.geolocate, this),
112
                    OpenLayers.Function.bind(this.failure, this),
113
                    this.geolocationOptions
114
                );
115
            } else {
116
                this.getCurrentLocation();
117
            }
118
            return true;
119
        }
120
        return false;
121
    },
122

    
123
    /**
124
     * Method: deactivate
125
     * Deactivates the control.
126
     *
127
     * Returns:
128
     * {Boolean} The control was effectively deactivated.
129
     */
130
    deactivate: function () {
131
        if (this.active && this.watchId !== null) {
132
            this.geolocation.clearWatch(this.watchId);
133
        }
134
        return OpenLayers.Control.prototype.deactivate.apply(
135
            this, arguments
136
        );
137
    },
138

    
139
    /**
140
     * Method: geolocate
141
     * Activates the control.
142
     *
143
     */
144
    geolocate: function (position) {
145
        var center = new OpenLayers.LonLat(
146
            position.coords.longitude,
147
            position.coords.latitude
148
        ).transform(
149
            new OpenLayers.Projection("EPSG:4326"),
150
            this.map.getProjectionObject()
151
        );
152
        if (this.bind) {
153
            this.map.setCenter(center);
154
        }
155
        this.events.triggerEvent("locationupdated", {
156
            position: position,
157
            point: new OpenLayers.Geometry.Point(
158
                center.lon, center.lat
159
            )
160
        });
161
    },
162

    
163
    /**
164
     * APIMethod: getCurrentLocation
165
     *
166
     * Returns:
167
     * {Boolean} Returns true if a event will be fired (successfull
168
     * registration)
169
     */
170
    getCurrentLocation: function() {
171
        if (!this.active || this.watch) {
172
            return false;
173
        }
174
        this.geolocation.getCurrentPosition(
175
            OpenLayers.Function.bind(this.geolocate, this),
176
            OpenLayers.Function.bind(this.failure, this),
177
            this.geolocationOptions
178
        );
179
        return true;
180
    },
181

    
182
    /**
183
     * Method: failure
184
     * method called on browser's geolocation failure
185
     *
186
     */
187
    failure: function (error) {
188
        this.events.triggerEvent("locationfailed", {error: error});
189
    },
190

    
191
    CLASS_NAME: "OpenLayers.Control.Geolocate"
192
});
(10-10/45)