-
Notifications
You must be signed in to change notification settings - Fork 24
/
geo-location.html
209 lines (175 loc) · 4.82 KB
/
geo-location.html
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<!-- Copyright Eric Bidelman <[email protected]> -->
<link rel="import" href="../polymer/polymer.html">
<!-- TODO: decide if we need to cache results across instances. -->
<!--
Geolocation API Polymer web component.
Example to get the device geolocation values:
```html
<geo-location latitude="{{latitude}}" longitude="{{longitude}}"></geo-location>
```
Continuous update the device geolocation values with high accuracy, and center Google Maps map and marker to the current location:
TODO: change the API key to your own.
```html
<geo-location watch-pos high-accuracy latitude="{{latitude}}" longitude="{{longitude}}"></geo-location>
<google-map latitude="[[latitude]]" longitude="[[longitude]]" api-key="AIzaSyD3E1D9b-Z7ekrT3tbhl_dy8DCXuIuDDRc">
<google-map-marker slot="markers" latitude="[[latitude]]" longitude="[[longitude]]"></google-map-marker>
</google-map>
```
@demo demo/index.html
-->
<script>
Polymer({
is: 'geo-location',
hostAttributes: {
hidden: true
},
properties: {
/**
* The latitude of the current position.
*/
latitude: {
type: Number,
notify: true,
reflectToAttribute: true,
readOnly: true,
value: null
},
/**
* The longitude of the current position.
*/
longitude: {
type: Number,
notify: true,
reflectToAttribute: true,
readOnly: true,
value: null
},
/**
* If true, the element won't be active at all.
*/
idle: {
type: Boolean,
value: false
},
/**
* If true, the latitude/longitude update as the device changes position.
* If not set, the latitude/longitude are provided once.
*/
watchPos: {
type: Boolean,
value: false
},
/**
* If true, enables high accuracy GPS.
*/
highAccuracy: {
type: Boolean,
value: false
},
/**
* The maximumAge option in the Gelocation API.
*/
maximumAge: {
type: Number,
value: 0
},
/**
* The timeout option in the Gelocation API.
*/
timeout: {
type: Number,
value: 5000
},
/**
* Geolocation API position object
*/
position: {
type: Object,
notify: true,
readOnly: true,
value: null
}
},
observers: [
'fetch(idle, watchPos, highAccuracy, timeout, maximumAge)'
],
/**
* Fired when the Geolocation API returns an error.
*
* @event geo-error
* @param {Object} detail
* @param {boolean} detail.error The error message.
*/
/**
* Fired when the Geolocation API returns a position result.
*
* @event geo-response
* @param {Object} detail
* @param {Position} position The raw position object returned by the Geolocation API.
* @param {Number} detail.latitude Latitude of the current position.
* @param {Number} detail.longitude Longitude of the current position.
*/
detached: function () {
this._clearWatch(this._watch);
},
/**
* Stop updating latitude/longitude as the device changes position.
* @param {Number} watch watch ID value.
*/
_clearWatch: function (watch) {
if (watch) {
navigator.geolocation.clearWatch(watch);
this._watch = null;
}
},
clear: function () {
this._setPosition(null);
this._setLatitude(null);
this._setLongitude(null);
},
fetch: function () {
this._clearWatch(this._watch);
if (this.idle) {
return;
}
var success = this._onPosition.bind(this);
var error = this._onError.bind(this);
var options = {
enableHighAccuracy: this.highAccuracy,
timeout: this.timeout,
maximumAge: this.maximumAge
};
if (this.watchPos) {
this._watch = navigator.geolocation.watchPosition(success, error, options);
} else {
navigator.geolocation.getCurrentPosition(success, error, options);
}
},
/**
* Success callback when the Geolocation API returns results.
*
* @param {Position} pos A position object from the Geolocation API.
*/
_onPosition: function(pos) {
this._setPosition(pos);
this._setLatitude(pos.coords.latitude);
this._setLongitude(pos.coords.longitude);
this.fire('geo-response', {
latitude: this.latitude,
longitude: this.longitude,
position: pos
});
},
/**
* Error callback when the Geolocation API returns an error.
*
* @param {Position} err The error that was returned.
*/
_onError: function(err) {
this.fire('geo-error', {
error: err.code + ': ' + err.message,
code: err.code
});
}
});
</script>