Skip to content

Commit

Permalink
Add reverse geocoding support. closes #25
Browse files Browse the repository at this point in the history
  • Loading branch information
daspecster committed Jul 10, 2016
1 parent c8bd850 commit 1ebc4f8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 26 deletions.
16 changes: 14 additions & 2 deletions README.md
Expand Up @@ -10,12 +10,12 @@ Seamlessly integrate [Ziptastic!](https://www.getziptastic.com) with jQuery
Can be used to query for a specific zip code.

```js
$.ziptastic(48867, function(country, state, stateCode, city, zip) {
$.ziptastic('US', 48867, 'your-api-key-here', function(country, state, stateCode, city, zip) {
console.log(country, state, stateCode, city, zip);
});
```

#### Input Keyup Wrapper
#### Input Keyup Wrapper with forward geocoding (postal code)

```js
var duration = 500;
Expand Down Expand Up @@ -52,3 +52,15 @@ elements.zip.ziptastic(options)
});
});
```

#### Using Reverse Geocoding

Just set `reverseGeo` to `true` in the `options` object.

```js
var options = {
"key": "<your-api-key-here>",
"reverseGeo": true,
"country": "US"
}
```
2 changes: 1 addition & 1 deletion demo.html
Expand Up @@ -59,7 +59,7 @@

// Initialize the ziptastic and bind to the change of zip code
var options = {
"key": "",
"key": "your-api-key-here",
"country": "US"
}
elements.zip.ziptastic(options)
Expand Down
83 changes: 60 additions & 23 deletions jquery.ziptastic.js
Expand Up @@ -3,28 +3,27 @@
var zipValid = {
us: /[0-9]{5}(-[0-9]{4})?/
};
var protocol = '';
if (location.protocol == 'file:') {
protocol = 'https://';
} else {
protocol = location.protocol;
}
var referrer = document.location.origin + document.location.pathname;

$.ziptastic = function(country, zip, key, callback){
$.ziptastic = function(country, zip, key, callback) {
country = country || 'US';
if (!key) {
alert("Please register for an API key at GetZiptastic.com.");
return;
}
if (!key) {
alert("Please register for an API key at GetZiptastic.com.");
return;
}

country = country.toUpperCase();
if(!requests[country]) {
requests[country] = {};
}

if(!requests[country][zip]) {
var protocol = '';
if (location.protocol == 'file:') {
protocol = 'https://';
} else {
protocol = location.protocol;
}

var referrer = document.location.origin + document.location.pathname;
requests[country][zip] = $.ajax({
url: protocol + '//zip.getziptastic.com/v3/' + country + '/' + zip,
headers: {'x-referrer': referrer, 'x-key': key},
Expand All @@ -44,26 +43,64 @@

requests[country][zip] = data_temp[key];
callback(data_temp[key].country, data_temp[key].state,
data_temp[key].state_short, data_temp[key].city, zip);
data_temp[key].state_short, data_temp[key].city, zip);
}
});
}
// Allow for binding to the deferred object
return requests[country][zip];
};

$.reverseZiptastic = function(latitude, longitude, key, callback) {
var request = $.ajax({
url: protocol + '//zip.getziptastic.com/v3/reverse/' + latitude + '/' + longitude + '/5000',
headers: {'x-referrer': referrer, 'x-key': key},
contentType: "application/json",
type: 'GET',
dataType: 'json',
error: function(e) { console.log('There was an error. ' + e.message ); }
});

request.done(function(data) {
if (typeof callback == 'function') {
callback(data[0].country, data[0].state,
data[0].state_short, data[0].city, zip);
}
});
}

$.fn.ziptastic = function( options ) {
return this.each(function() {
var ele = $(this);
ele.on('keyup change', function() {
var zip = ele.val();
if(zipValid.us.test(zip)) {
$.ziptastic(options.country, zip, options.key, function(country, state, state_short, city) {
// Trigger the updated information
ele.trigger('zipChange', [country, state, state_short, city, zip]);
});
}
});
if (options.reverseGeo == true) {
var geoSuccess = function(position) {
$.reverseZiptastic(position.coords.latitude, position.coords.longitude, options.key, function(country, state, state_short, city){
ele.trigger('zipChange', [country, state, state_short, city, zip]);
});
}

var geoError = function(e) {
error(e);
}

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(geoSuccess, geoError);
} else {
console.log('Geolocation is not supported by your browser.');
}

} else {

ele.on('keyup change', function() {
var zip = ele.val();
if(zipValid.us.test(zip)) {
$.ziptastic(options.country, zip, options.key, function(country, state, state_short, city) {
// Trigger the updated information
ele.trigger('zipChange', [country, state, state_short, city, zip]);
});
}
});
}
});
};
})( jQuery );

0 comments on commit 1ebc4f8

Please sign in to comment.