Created
April 7, 2011 23:15
-
-
Save ashaw/908989 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var Geocoder = function(address, cb) { | |
this.address = address; | |
this.geocoder = new google.maps.Geocoder(); | |
this.geocode(cb); | |
}; | |
Geocoder.prototype = { | |
geocode : function(cb) { | |
if (this.address === "") return false; | |
var that = this; | |
this.geocoder.geocode({'address': that.address, 'region' : 'us'}, function(results, status){ | |
if (results.length === 0) { | |
return cb({ error : 'We couldn\'t find any results for that address. Please try again'}); | |
} | |
var geom = results[0]['geometry']; | |
if (status == google.maps.GeocoderStatus.OK) { | |
that.location_type = geom['location_type']; | |
var location = geom['location']; | |
if (that.isValidLocationType()) { | |
that.ll = { | |
lat : location.lat(), | |
lng : location.lng(), | |
location_type : that.location_type | |
}; | |
} else { | |
that.ll = { | |
error : "Can't find specific enough results. Please try again." | |
}; | |
} | |
if (cb) | |
cb(that.ll); | |
} | |
}); | |
}, | |
isValidLocationType : function() { | |
if (this.isValidZip() || this.hasComma()) { | |
return true; | |
} else if (this.location_type === "RANGE_INTERPOLATED" || this.location_type === "ROOFTOP" ) { | |
return true; | |
} else { | |
return false; | |
} | |
}, | |
isValidZip : function() { | |
return !_.isNull(this.address.match(/^[0-9]{5}$/)); | |
}, | |
hasComma : function() { | |
return !_.isNull(this.address.match(/,/)); | |
} | |
}; | |
window.Geocoder = Geocoder; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment