diff options
author | Joar Wandborg <git@wandborg.com> | 2012-01-10 01:54:37 +0100 |
---|---|---|
committer | Joar Wandborg <git@wandborg.com> | 2012-01-25 23:42:04 +0100 |
commit | c5ba5b0456a711d157e317f220e9c739226e7f50 (patch) | |
tree | 2800568ac8e484649a978e0fe7e682a8bcfe20d4 /extlib/leaflet/src/map/ext/Map.Geolocation.js | |
parent | c47a03b909ecd97cab5b144d0cab007b62b92a90 (diff) | |
download | mediagoblin-c5ba5b0456a711d157e317f220e9c739226e7f50.tar.lz mediagoblin-c5ba5b0456a711d157e317f220e9c739226e7f50.tar.xz mediagoblin-c5ba5b0456a711d157e317f220e9c739226e7f50.zip |
Installed leaflet in extlib
Diffstat (limited to 'extlib/leaflet/src/map/ext/Map.Geolocation.js')
-rw-r--r-- | extlib/leaflet/src/map/ext/Map.Geolocation.js | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/extlib/leaflet/src/map/ext/Map.Geolocation.js b/extlib/leaflet/src/map/ext/Map.Geolocation.js new file mode 100644 index 00000000..328662b9 --- /dev/null +++ b/extlib/leaflet/src/map/ext/Map.Geolocation.js @@ -0,0 +1,69 @@ +/*
+ * Provides L.Map with convenient shortcuts for W3C geolocation.
+ */
+
+L.Map.include({
+ locate: function(/*Object*/ options) { + // W3C Geolocation API Spec position options, http://dev.w3.org/geo/api/spec-source.html#position-options
+ var opts = {timeout: 10000};
+ L.Util.extend(opts, options);
+
+ if (navigator.geolocation) {
+ navigator.geolocation.getCurrentPosition(
+ L.Util.bind(this._handleGeolocationResponse, this),
+ L.Util.bind(this._handleGeolocationError, this),
+ opts);
+ } else {
+ this.fire('locationerror', {
+ code: 0,
+ message: "Geolocation not supported."
+ });
+ }
+ return this;
+ },
+
+ locateAndSetView: function(maxZoom, options) {
+ this._setViewOnLocate = true;
+ this._maxLocateZoom = maxZoom || Infinity;
+ return this.locate(options);
+ },
+
+ _handleGeolocationError: function(error) {
+ var c = error.code,
+ message = (c == 1 ? "permission denied" :
+ (c == 2 ? "position unavailable" : "timeout"));
+
+ if (this._setViewOnLocate) {
+ this.fitWorld();
+ this._setViewOnLocate = false;
+ }
+
+ this.fire('locationerror', {
+ code: c,
+ message: "Geolocation error: " + message + "."
+ });
+ },
+
+ _handleGeolocationResponse: function(pos) {
+ var latAccuracy = 180 * pos.coords.accuracy / 4e7,
+ lngAccuracy = latAccuracy * 2,
+ lat = pos.coords.latitude,
+ lng = pos.coords.longitude;
+
+ var sw = new L.LatLng(lat - latAccuracy, lng - lngAccuracy),
+ ne = new L.LatLng(lat + latAccuracy, lng + lngAccuracy),
+ bounds = new L.LatLngBounds(sw, ne);
+
+ if (this._setViewOnLocate) {
+ var zoom = Math.min(this.getBoundsZoom(bounds), this._maxLocateZoom);
+ this.setView(bounds.getCenter(), zoom);
+ this._setViewOnLocate = false;
+ }
+
+ this.fire('locationfound', {
+ latlng: new L.LatLng(lat, lng),
+ bounds: bounds,
+ accuracy: pos.coords.accuracy
+ });
+ }
+});
\ No newline at end of file |