-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjquery.geo.js
More file actions
113 lines (100 loc) · 3.75 KB
/
jquery.geo.js
File metadata and controls
113 lines (100 loc) · 3.75 KB
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
/*!
* jQuery-Geo jQuery plugin v0.0.1
*
* Copyright 2010 by Dan Singerman <dansingerman@gmail.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
(function($){
var publicMethods = {
locate : function( successCallback, failureCallback ) {
if ( navigator.geolocation ) {
// browser supports W3C geolocation API
navigator.geolocation.getCurrentPosition(
function( position ){
processLocation( position , successCallback);
},
function(err){
processError( err, failureCallback );
});
}
else if ( window.google && google.gears ) {
// this provides support for Android versions < 2.0
var geo = google.gears.factory.create( 'beta.geolocation' );
geo.getCurrentPosition(
function( position ){
processLocation( position , successCallback );
},
function(err){
processError( err , failureCallback );
});
}
else {
processError( {
code: 99
}, failureCallback );
}
},
/* Haversine formula calculations taken from
http://www.movable-type.co.uk/scripts/latlong.html
*/
distance : function( lat1 , lon1 , lat2 , lon2) {
var R = 6371; // km
var dLat = (lat2-lat1) * (Math.PI / 180);
var dLon = (lon2-lon1) * (Math.PI / 180);
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1 * (Math.PI / 180)) * Math.cos(lat2 * (Math.PI / 180)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return d * 1000;
}
};
// success!! we have a latitude and longitude
var processLocation = function ( position , callback ) {
callback( position );
};
/* we have failed to get a location
* error has made up status code of 99 if device does not support W3C geolocation
* otherwise error is instance of PositionError
* http://dev.w3.org/geo/api/spec-source.html#position_error_interface */
var processError = function ( error, callback ) {
var message;
switch( error.code ) {
case error.TIMEOUT:
message = "Geolocation Timeout";
break;
case error.PERMISSION_DENIED:
message = "Permission Denied";
break;
case error.POSITION_UNAVAILABLE:
message = "Position Unavailable";
break;
default:
message = "Geolocation not supported by jQuery-Geo for your device";
}
callback( error, message );
};
// best practice jQuery plugin namespacing
$.geo = function( method ) {
if ( publicMethods[method] ) {
return publicMethods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return publicMethods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist in jQuery.geo' );
}
};
})(jQuery);