|
| 1 | +<dom-module id="device-location"> |
| 2 | +<script> |
| 3 | + |
| 4 | +Polymer({ |
| 5 | + is: "device-location", |
| 6 | + properties: { |
| 7 | + lat : { |
| 8 | + type: Number, |
| 9 | + value: 0, |
| 10 | + reflectToAttribute: false |
| 11 | + }, |
| 12 | + lon : { |
| 13 | + type: Number, |
| 14 | + value: 0, |
| 15 | + reflectToAttribute: false |
| 16 | + }, |
| 17 | + centered : { |
| 18 | + type: Boolean, |
| 19 | + reflectToAttribute: true |
| 20 | + } |
| 21 | + }, |
| 22 | + observers: [ |
| 23 | + '_toggleCentered(centered)' |
| 24 | + ], |
| 25 | + _toggleCentered: function (centered) { |
| 26 | + if (centered) { |
| 27 | + // TODO create a point feature on the controls pane at the map center |
| 28 | + // DONE register a watchPosition function to move the map as the device |
| 29 | + // location changes |
| 30 | + var self = this, |
| 31 | + options = { |
| 32 | + enableHighAccuracy: true, |
| 33 | + timeout: 5000, |
| 34 | + maximumAge: 0 |
| 35 | + }; |
| 36 | + function moveTo(position) { |
| 37 | + self.lat = position.coords.latitude; |
| 38 | + self.lon = position.coords.longitude; |
| 39 | + if (self.parentNode.zoomTo && self.parentNode._map) { |
| 40 | + // TODO when the zoomTo bug is fixed, remove zoom+1 |
| 41 | + self.parentNode.zoomTo(self.lat,self.lon,self.parentNode.zoom+1); |
| 42 | + } |
| 43 | + }; |
| 44 | + this._watchID = navigator.geolocation.watchPosition(moveTo,null,options); |
| 45 | + } else { |
| 46 | + this._deleteWatch(); |
| 47 | + // TODO replace the marker on the control pane at the center of the map |
| 48 | + // with an actual point feature , or simply move that point from |
| 49 | + // the control pane to the vector pane |
| 50 | + |
| 51 | + // TODO register a navigator.geolocation.watchPosition function to |
| 52 | + // update the location of the above point feature |
| 53 | + } |
| 54 | + }, |
| 55 | + detached: function() { |
| 56 | + this._deleteWatch(); |
| 57 | + // TODO if there is a point associated to this control, remove/delete it |
| 58 | + }, |
| 59 | + attached: function() { |
| 60 | + this._deleteWatch(); |
| 61 | + this._createWatch(); |
| 62 | + if (this.parentNode.controls) { |
| 63 | + // add the device-location radio button control to the controls pane |
| 64 | + this._map = this.parentNode._map; |
| 65 | + } |
| 66 | + }, |
| 67 | + ready: function() { |
| 68 | + if (this.centered) { |
| 69 | + var self = this, |
| 70 | + options = { |
| 71 | + enableHighAccuracy: true, |
| 72 | + timeout: 5000, |
| 73 | + maximumAge: 0 |
| 74 | + }; |
| 75 | + function moveTo(position) { |
| 76 | + self.lat = position.coords.latitude; |
| 77 | + self.lon = position.coords.longitude; |
| 78 | + if (self.parentNode.zoomTo && self.parentNode._map) { |
| 79 | + // TODO when the zoomTo bug is fixed, remove zoom+1 |
| 80 | + self.parentNode.zoomTo(self.lat,self.lon,self.parentNode.zoom+1); |
| 81 | + } |
| 82 | + }; |
| 83 | + this._watchID = navigator.geolocation.watchPosition(moveTo,null,options); |
| 84 | + } |
| 85 | + }, |
| 86 | +// attached: function() { |
| 87 | +// this._deleteWatch(); |
| 88 | +// // could have a _watchID from being created without being detached |
| 89 | +// // i.e. never having been attached before... |
| 90 | +// if (this.centered) { |
| 91 | +// // create point on the control layer at the map center... |
| 92 | +// |
| 93 | +// } |
| 94 | +// |
| 95 | +// }, |
| 96 | + _deleteWatch: function() { |
| 97 | + if (this._watchID) { |
| 98 | + navigator.geolocation.clearWatch(this._watchID); |
| 99 | + delete this._watchID; |
| 100 | + } |
| 101 | + }, |
| 102 | + _createWatch: function() { |
| 103 | + if (this.centered) { |
| 104 | + var self = this, |
| 105 | + options = { |
| 106 | + enableHighAccuracy: true, |
| 107 | + timeout: 5000, |
| 108 | + maximumAge: 0 |
| 109 | + }; |
| 110 | + function moveTo(position) { |
| 111 | + self.lat = position.coords.latitude; |
| 112 | + self.lon = position.coords.longitude; |
| 113 | + if (self.parentNode.zoomTo && self.parentNode._map) { |
| 114 | + // TODO when the zoomTo bug is fixed, remove zoom+1 |
| 115 | + self.parentNode.zoomTo(self.lat,self.lon,self.parentNode.zoom+1); |
| 116 | + } |
| 117 | + }; |
| 118 | + this._watchID = navigator.geolocation.watchPosition(moveTo,null,options); |
| 119 | + } |
| 120 | + } |
| 121 | +}); |
| 122 | +</script> |
| 123 | +</dom-module> |
0 commit comments