From 1ebc4f81da259f4258bf900270a8191b4be94f47 Mon Sep 17 00:00:00 2001 From: Thomas Schultz Date: Sun, 10 Jul 2016 12:39:08 -0400 Subject: [PATCH] Add reverse geocoding support. closes #25 --- README.md | 16 +++++++-- demo.html | 2 +- jquery.ziptastic.js | 83 ++++++++++++++++++++++++++++++++------------- 3 files changed, 75 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 01fa89a..ee85f36 100644 --- a/README.md +++ b/README.md @@ -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; @@ -52,3 +52,15 @@ elements.zip.ziptastic(options) }); }); ``` + +#### Using Reverse Geocoding + +Just set `reverseGeo` to `true` in the `options` object. + +```js +var options = { + "key": "", + "reverseGeo": true, + "country": "US" +} +``` diff --git a/demo.html b/demo.html index 126b00e..c2fab56 100644 --- a/demo.html +++ b/demo.html @@ -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) diff --git a/jquery.ziptastic.js b/jquery.ziptastic.js index 87e328c..dcf3640 100644 --- a/jquery.ziptastic.js +++ b/jquery.ziptastic.js @@ -3,13 +3,20 @@ 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]) { @@ -17,14 +24,6 @@ } 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}, @@ -44,7 +43,7 @@ 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); } }); } @@ -52,18 +51,56 @@ 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 );