├── License.txt ├── README.md ├── bower.json ├── earth.png ├── index.html ├── jquery.geolocation.js ├── jquery.geolocation.min.js └── package.json /License.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Manuel Bieh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # jQuery Geolocation 2 | 3 | A small jQuery plugin which acts as a simplification of the [Geolocation API](http://dev.w3.org/geo/api/spec-source.html). 4 | 5 | Instead of using navigator.geolocation.getCurrentPosition you can now just use the jQuery methods `$.geolocation.get()` or `$.geolocation.watch()`. 6 | 7 | Contrary to the standard API the only parameter the functions expect is an object with three properties in no particular order: `success`, `error`, `options`. For `success` and `error` you can also use their alias properties `win` (or `done`) and `fail`: `$.geolocation.get({win: function() {}, fail: function() {}, options);` 8 | 9 | You can also use `$.geolocation.getCurrentPosition(success, error, options)` to get native API feeling if this makes you happier. In conjunction with my [Geolocation API polyfill](https://github.com/manuelbieh/Geolocation-API-Polyfill) this also works with some non-standard Geolocation APIs like Google Gears or Blackberry Location. 10 | 11 | ## Usage 12 | 13 | ### $.geolocation.clearWatch(integer watchID) 14 | Stops tracking of the user for the according watchID. 15 | 16 | ### $.geolocation.get(object config) 17 | Get the current position of the user 18 | 19 | #### Config properties 20 | 21 | 46 | 47 | 48 | ### $.geolocation.getCurrentPosition(callback success, callback error, object settings) 49 | Get the current position of the user (API standard behavior) 50 | 51 | #### Parameters 52 | 53 | success Function to call if geolocation request was successful
54 | error Function to call if geolocation request failed
55 | options Options for the geolocation request 56 | 57 | 62 | 63 | ### $.geolocation.stop(integer watchID) 64 | 65 | Stops tracking of the user for the according watchID. 66 | 67 | ### $.geolocation.stopAll() 68 | 69 | Stops all running watchPosition callbacks. 70 | 71 | ### $.geolocation.watch(object config) 72 | 73 | Track the movement of the user 74 | Returns: watchID (Integer) 75 | 76 | #### Config properties 77 | 78 | 103 | 104 | 105 | ### $.geolocation.watchPosition(callback success, callback error, object settings) 106 | 107 | Track the movement of the user (API standard behavior) 108 | Returns: watchID (Integer) 109 | 110 | #### Parameters 111 | 112 | success Function to call if geolocation request was successful
113 | error Function to call if geolocation request failed
114 | options Options for the geolocation request 115 | 120 | 121 | 122 | ## Examples 123 |
function alertMyPosition(position) {
124 | 	alert("Your position is " + position.coords.latitude + ", " + position.coords.longitude);
125 | }
126 | 
127 | function noLocation(error) {
128 | 	alert("No location info available. Error code: " + error.code);
129 | }
130 | 
131 | $('#getPositionButton').on('click', function() {
132 | 	$.geolocation.get({win: alertMyPosition, fail: noLocation});
133 | });
134 | 
135 | $('#watchPositionButton').on('click', function() {
136 | 	// alertMyPosition is called each time the user's position changes
137 | 	myPosition = $.geolocation.watch({win: alertMyPosition}); 
138 | });
139 | 
140 | $('#stopButton').on('click', function() {
141 | 	$.geolocation.stop(myPosition);
142 | });
143 | 144 | ## Deferreds 145 | 146 | New in 1.1.0: 147 | jQuery Deferreds are now supported for `get` and `getCurrentPosition`. Just use: 148 | `$.geolocation.get().done(successCallback).fail(errorCallback);` 149 | 150 | Attention: Deferreds support is in beta state. 151 | 152 | ## Demo 153 | [You can find a demo here](http://manuel-bieh.de/publikationen/scripts/jquery/geolocation/) 154 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jquery-geolocation", 3 | "version": "1.1.1", 4 | "description": "jQuery plugin which acts as a simplification of the Geolocation API.", 5 | "license": "MIT", 6 | "ignore": [ 7 | "earth.png", 8 | "index.html" 9 | ], 10 | "dependencies": { 11 | "jquery": ">=1.6.0" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /earth.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manuelbieh/jQuery-Geolocation/8fdcea3c172d7dd1c6e8ab2dc36c511b94021daa/earth.png -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 57 | 58 | 121 | 122 | 123 | 124 | 125 |
126 | 127 |
128 | 129 |
130 | 131 |
132 | 133 |

jQuery Geolocation API simplification

134 | 135 |

Single position acquisition

136 |

137 | 138 | 139 |

140 |
141 | 142 |

Observe changes of your position

143 |

144 | 145 | 146 |

147 | 148 |
149 | 150 |
151 | 152 |
153 | 154 | 155 | -------------------------------------------------------------------------------- /jquery.geolocation.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * A simple jQuery Wrapper for Geolocation API 3 | * Supports Deferreds 4 | * 5 | * @author: Manuel Bieh 6 | * @url: http://www.manuel-bieh.de/ 7 | * @documentation: http://www.manuel-bieh.de/blog/geolocation-jquery-plugin 8 | * @version 1.1.0 9 | * @license MIT 10 | */ 11 | 12 | (function($) { 13 | 14 | $.extend({ 15 | 16 | geolocation: { 17 | 18 | watchIDs: [], 19 | 20 | get: function(arg1, arg2, arg3) { 21 | 22 | var o = {}; 23 | 24 | if(typeof arg1 === 'object') { 25 | o = $.geolocation.prepareOptions(arg1); 26 | } else { 27 | o = $.geolocation.prepareOptions({success: arg1, error: arg2, options: arg3}); 28 | } 29 | 30 | return $.geolocation.getCurrentPosition(o.success, o.error, o.options); 31 | 32 | }, 33 | 34 | getPosition: function(o) { 35 | return $.geolocation.get.call(this, o); 36 | }, 37 | 38 | getCurrentPosition: function(arg1, arg2, arg3) { 39 | 40 | var defer = $.Deferred(); 41 | 42 | if(typeof navigator.geolocation != 'undefined') { 43 | 44 | if(typeof arg1 === 'function') { 45 | 46 | navigator.geolocation.getCurrentPosition(arg1, arg2, arg3); 47 | 48 | } else { 49 | 50 | navigator.geolocation.getCurrentPosition(function() { 51 | defer.resolveWith(this, arguments); 52 | }, function() { 53 | defer.rejectWith(this, arguments); 54 | }, arg1 || arg3); 55 | 56 | return defer.promise(); 57 | 58 | } 59 | 60 | } else { 61 | 62 | var error = {"message": "No geolocation available"}; 63 | 64 | if(typeof arg2 === 'function') { 65 | arg2(error); 66 | } 67 | 68 | defer.rejectWith(this, [error]); 69 | return defer.promise(); 70 | 71 | } 72 | 73 | }, 74 | 75 | watch: function(o) { 76 | 77 | o = $.geolocation.prepareOptions(o); 78 | return $.geolocation.watchPosition(o.success, o.error, o.options); 79 | 80 | }, 81 | 82 | watchPosition: function(success, error, options) { 83 | 84 | if(typeof navigator.geolocation !== 'undefined') { 85 | 86 | watchID = navigator.geolocation.watchPosition(success, error, options); 87 | $.geolocation.watchIDs.push(watchID); 88 | return watchID; 89 | 90 | } else { 91 | 92 | error(); 93 | 94 | } 95 | 96 | }, 97 | 98 | stop: function(watchID) { 99 | 100 | if(typeof navigator.geolocation != 'undefined') { 101 | navigator.geolocation.clearWatch(watchID); 102 | } 103 | 104 | }, 105 | 106 | clearWatch: function(watchID) { 107 | $.geolocation.stop(watchID); 108 | }, 109 | 110 | stopAll: function() { 111 | 112 | $.each(jQuery.geolocation.watchIDs, function(key, value) { 113 | $.geolocation.stop(value); 114 | }); 115 | 116 | }, 117 | 118 | clearAll: function() { 119 | $.geolocation.stopAll(); 120 | }, 121 | 122 | prepareOptions: function(o) { 123 | 124 | o = o || {}; 125 | 126 | if(!!o.options === false) { 127 | 128 | o.options = { 129 | highAccuracy: false, 130 | maximumAge: 30000, // 30 seconds 131 | timeout: 60000 // 1 minute 132 | } 133 | 134 | } 135 | 136 | if(!!o.win !== false || !!o.done !== false) { 137 | o.success = o.win || o.done; 138 | } 139 | 140 | if(!!o.fail !== false) { 141 | o.error = o.fail; 142 | } 143 | 144 | return o; 145 | 146 | } 147 | 148 | } 149 | 150 | }); 151 | 152 | })(jQuery); -------------------------------------------------------------------------------- /jquery.geolocation.min.js: -------------------------------------------------------------------------------- 1 | /*! A simple jQuery Wrapper for Geolocation API 2 | * New in 1.1.0: 3 | * - Supports Deferreds! 4 | * 5 | * @author: Manuel Bieh 6 | * @url: http://www.manuel-bieh.de/ 7 | * @documentation: http://www.manuel-bieh.de/blog/geolocation-jquery-plugin 8 | * @version 1.1.0 9 | * @license MIT 10 | */ 11 | (function($){$.extend({geolocation:{watchIDs:[],get:function(arg1,arg2,arg3){var o={};if(typeof arg1==="object"){o=$.geolocation.prepareOptions(arg1)}else{o=$.geolocation.prepareOptions({success:arg1,error:arg2,options:arg3})}return $.geolocation.getCurrentPosition(o.success,o.error,o.options)},getPosition:function(o){return $.geolocation.get.call(this,o)},getCurrentPosition:function(arg1,arg2,arg3){var defer=$.Deferred();if(typeof navigator.geolocation!="undefined"){if(typeof arg1==="function"){navigator.geolocation.getCurrentPosition(arg1,arg2,arg3)}else{navigator.geolocation.getCurrentPosition(function(){defer.resolveWith(this,arguments)},function(){defer.rejectWith(this,arguments)},arg1||arg3);return defer.promise()}}else{var error={message:"No geolocation available"};if(typeof arg2==="function"){arg2(error)}defer.rejectWith(this,[error]);return defer.promise()}},watch:function(o){o=$.geolocation.prepareOptions(o);return $.geolocation.watchPosition(o.success,o.error,o.options)},watchPosition:function(success,error,options){if(typeof navigator.geolocation!=="undefined"){watchID=navigator.geolocation.watchPosition(success,error,options);$.geolocation.watchIDs.push(watchID);return watchID}else{error()}},stop:function(watchID){if(typeof navigator.geolocation!="undefined"){navigator.geolocation.clearWatch(watchID)}},clearWatch:function(watchID){$.geolocation.stop(watchID)},stopAll:function(){$.each(jQuery.geolocation.watchIDs,function(key,value){$.geolocation.stop(value)})},clearAll:function(){$.geolocation.stopAll()},prepareOptions:function(o){o=o||{};if(!!o.options===false){o.options={highAccuracy:false,maximumAge:3e4,timeout:6e4}}if(!!o.win!==false||!!o.done!==false){o.success=o.win||o.done}if(!!o.fail!==false){o.error=o.fail}return o}}})})(jQuery); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": " jQuery-Geolocation", 3 | "filename": "jquery.geolocation.js", 4 | "version": "1.1.1", 5 | "description": "jQuery plugin which acts as a simplification of the W3C Geolocation API ", 6 | "author": "Manuel Bieh", 7 | "homepage": "http://www.manuel-bieh.de", 8 | "keywords": [ 9 | "jquery", 10 | "geolocation" 11 | ], 12 | "licenses": [{ 13 | "type": "MIT" 14 | }], 15 | "repository": { 16 | "type": "git", 17 | "url": "git://github.com/manuelbieh/jQuery-Geolocation.git" 18 | } 19 | } 20 | --------------------------------------------------------------------------------