|
3 | 3 | var zipValid = { |
4 | 4 | us: /[0-9]{5}(-[0-9]{4})?/ |
5 | 5 | }; |
| 6 | + var protocol = ''; |
| 7 | + if (location.protocol == 'file:') { |
| 8 | + protocol = 'https://'; |
| 9 | + } else { |
| 10 | + protocol = location.protocol; |
| 11 | + } |
| 12 | + var referrer = document.location.origin + document.location.pathname; |
6 | 13 |
|
7 | | - $.ziptastic = function(country, zip, key, callback){ |
| 14 | + $.ziptastic = function(country, zip, key, callback) { |
8 | 15 | country = country || 'US'; |
9 | | - if (!key) { |
10 | | - alert("Please register for an API key at GetZiptastic.com."); |
11 | | - return; |
12 | | - } |
| 16 | + if (!key) { |
| 17 | + alert("Please register for an API key at GetZiptastic.com."); |
| 18 | + return; |
| 19 | + } |
13 | 20 |
|
14 | 21 | country = country.toUpperCase(); |
15 | 22 | if(!requests[country]) { |
16 | 23 | requests[country] = {}; |
17 | 24 | } |
18 | 25 |
|
19 | 26 | if(!requests[country][zip]) { |
20 | | - var protocol = ''; |
21 | | - if (location.protocol == 'file:') { |
22 | | - protocol = 'https://'; |
23 | | - } else { |
24 | | - protocol = location.protocol; |
25 | | - } |
26 | | - |
27 | | - var referrer = document.location.origin + document.location.pathname; |
28 | 27 | requests[country][zip] = $.ajax({ |
29 | 28 | url: protocol + '//zip.getziptastic.com/v3/' + country + '/' + zip, |
30 | 29 | headers: {'x-referrer': referrer, 'x-key': key}, |
|
44 | 43 |
|
45 | 44 | requests[country][zip] = data_temp[key]; |
46 | 45 | callback(data_temp[key].country, data_temp[key].state, |
47 | | - data_temp[key].state_short, data_temp[key].city, zip); |
| 46 | + data_temp[key].state_short, data_temp[key].city, zip); |
48 | 47 | } |
49 | 48 | }); |
50 | 49 | } |
51 | 50 | // Allow for binding to the deferred object |
52 | 51 | return requests[country][zip]; |
53 | 52 | }; |
54 | 53 |
|
| 54 | + $.reverseZiptastic = function(latitude, longitude, key, callback) { |
| 55 | + var request = $.ajax({ |
| 56 | + url: protocol + '//zip.getziptastic.com/v3/reverse/' + latitude + '/' + longitude + '/5000', |
| 57 | + headers: {'x-referrer': referrer, 'x-key': key}, |
| 58 | + contentType: "application/json", |
| 59 | + type: 'GET', |
| 60 | + dataType: 'json', |
| 61 | + error: function(e) { console.log('There was an error. ' + e.message ); } |
| 62 | + }); |
| 63 | + |
| 64 | + request.done(function(data) { |
| 65 | + if (typeof callback == 'function') { |
| 66 | + callback(data[0].country, data[0].state, |
| 67 | + data[0].state_short, data[0].city, zip); |
| 68 | + } |
| 69 | + }); |
| 70 | + } |
| 71 | + |
55 | 72 | $.fn.ziptastic = function( options ) { |
56 | 73 | return this.each(function() { |
57 | 74 | var ele = $(this); |
58 | | - ele.on('keyup change', function() { |
59 | | - var zip = ele.val(); |
60 | | - if(zipValid.us.test(zip)) { |
61 | | - $.ziptastic(options.country, zip, options.key, function(country, state, state_short, city) { |
62 | | - // Trigger the updated information |
| 75 | + if (options.reverseGeo == true) { |
| 76 | + var geoSuccess = function(position) { |
| 77 | + $.reverseZiptastic(position.coords.latitude, position.coords.longitude, options.key, function(country, state, state_short, city){ |
63 | 78 | ele.trigger('zipChange', [country, state, state_short, city, zip]); |
64 | 79 | }); |
65 | 80 | } |
66 | | - }); |
| 81 | + |
| 82 | + var geoError = function(e) { |
| 83 | + error(e); |
| 84 | + } |
| 85 | + |
| 86 | + if (navigator.geolocation) { |
| 87 | + navigator.geolocation.getCurrentPosition(geoSuccess, geoError); |
| 88 | + } else { |
| 89 | + console.log('Geolocation is not supported by your browser.'); |
| 90 | + } |
| 91 | + |
| 92 | + } else { |
| 93 | + |
| 94 | + ele.on('keyup change', function() { |
| 95 | + var zip = ele.val(); |
| 96 | + if(zipValid.us.test(zip)) { |
| 97 | + $.ziptastic(options.country, zip, options.key, function(country, state, state_short, city) { |
| 98 | + // Trigger the updated information |
| 99 | + ele.trigger('zipChange', [country, state, state_short, city, zip]); |
| 100 | + }); |
| 101 | + } |
| 102 | + }); |
| 103 | + } |
67 | 104 | }); |
68 | 105 | }; |
69 | 106 | })( jQuery ); |
0 commit comments