var geocoder; //this is a global variable
function initialize() {
geocoder = new google.maps.Geocoder();
}
function loadScript() {
var mapScript = document.createElement('script');
mapScript.type = 'text/javascript';
mapScript.src = 'http://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize';
document.body.appendChild(mapScript);
}
function codeAddress() {
var address = document.getElementById('streetAddress').value;
var city = document.getElementById('city').value;
var state = document.getElementById('state').value;
if (address === '') {
alert('You must enter a street address!');
return false;
}
address = address + ' ' + city + ' ' + state;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
alert(status + ' ' + google.maps.GeocoderStatus.OK);
//whichZone(results[0].geometry.location);
return true;
} else {
showLightbox(320,130,'10px','notFoundChoice');
document.getElementById('tryAgain').focus();
return false;
}
});
I haven't done much with this, but there are other results fields returned:
http://code.google.com/apis/maps/documentation/javascript/services.html#GeocodingResults
Do types and location_type help to determine what the geocoder is
telling you? I guess for a real address you would get either ROOFTOP
or RANGE_INTERPOLATED, and if it was limited to a city centre you
wouldn't get those.
I think ZERO_RESULTS will be the response where the geocoder gives up
completely.
The geocoder is working as it should; it's doing the best it can to
provide some kind of location from mangled input. It's not an address
validation service.
But, as Andrew says, you can examine just what kind of guess it has
made.
if (status === google.maps.GeocoderStatus.OK && (results[0].geometry.location_type === 'ROOFTOP' || results[0].geometry.location_type === 'RANGE_INTERPOLATED'))
I think that's the size of it; so far as I can see the geocoder is
provided by Google to extract some/any kind of location from user's
mangled input, which makes it inherently unsuitable for address
validation (or the partial validation you require)
> Honestly, now that we are checking the other results, it's not really a
> problem.
Understood
> My frustration was that it *changed.
Yep, they do that. For most use-cases it would be an enhancement.
One of the real maps gurus often said "never trust a geocoder",
they're never more than a best-guesser.
Only the end user knows what they really mean, hence maps.google.com
"Did you mean?" feature. API developers can produce their own
tailored version (e.g. in your case it might be geographically
limited)