Skip to content

Commit 27ceb88

Browse files
authored
Merge pull request #30 from Ziptastic/add-reverse-geocoding
Add reverse geocoding support.
2 parents c8bd850 + 5173d09 commit 27ceb88

3 files changed

Lines changed: 72 additions & 23 deletions

File tree

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ Seamlessly integrate [Ziptastic!](https://www.getziptastic.com) with jQuery
1010
Can be used to query for a specific zip code.
1111

1212
```js
13-
$.ziptastic(48867, function(country, state, stateCode, city, zip) {
13+
$.ziptastic('US', 48867, 'your-api-key-here', function(country, state, stateCode, city, zip) {
1414
console.log(country, state, stateCode, city, zip);
1515
});
1616
```
1717

18-
#### Input Keyup Wrapper
18+
#### Input Keyup Wrapper with forward geocoding (postal code)
1919

2020
```js
2121
var duration = 500;
@@ -52,3 +52,15 @@ elements.zip.ziptastic(options)
5252
});
5353
});
5454
```
55+
56+
#### Using Reverse Geocoding
57+
58+
Just set `reverseGeo` to `true` in the `options` object.
59+
60+
```js
61+
var options = {
62+
"key": "<your-api-key-here>",
63+
"reverseGeo": true,
64+
"country": "US"
65+
}
66+
```

demo.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959

6060
// Initialize the ziptastic and bind to the change of zip code
6161
var options = {
62-
"key": "",
62+
"key": "your-api-key-here",
6363
"country": "US"
6464
}
6565
elements.zip.ziptastic(options)

jquery.ziptastic.js

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,27 @@
33
var zipValid = {
44
us: /[0-9]{5}(-[0-9]{4})?/
55
};
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;
613

7-
$.ziptastic = function(country, zip, key, callback){
14+
$.ziptastic = function(country, zip, key, callback) {
815
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+
}
1320

1421
country = country.toUpperCase();
1522
if(!requests[country]) {
1623
requests[country] = {};
1724
}
1825

1926
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;
2827
requests[country][zip] = $.ajax({
2928
url: protocol + '//zip.getziptastic.com/v3/' + country + '/' + zip,
3029
headers: {'x-referrer': referrer, 'x-key': key},
@@ -44,26 +43,64 @@
4443

4544
requests[country][zip] = data_temp[key];
4645
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);
4847
}
4948
});
5049
}
5150
// Allow for binding to the deferred object
5251
return requests[country][zip];
5352
};
5453

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+
5572
$.fn.ziptastic = function( options ) {
5673
return this.each(function() {
5774
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){
6378
ele.trigger('zipChange', [country, state, state_short, city, zip]);
6479
});
6580
}
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+
}
67104
});
68105
};
69106
})( jQuery );

0 commit comments

Comments
 (0)