Hi I'm trying to find out if an address exists in a City+Province+Country using google's maps API v3 geocoding webservice. I've seen that Google always returns something. For example, if I try to find an existing address: "Colon 100, Córdoba, Córdoba, Argentina" using: http://maps.googleapis.com/maps/api/geocode/json?address=Colon%20100%20,C%C3%B3rdoba,C%C3%B3rdoba,Argentina&sensor=false I get a something. But when I try to find and non existing address like, "nonexistingaddres 2, Córdoba, Córdoba, Argentina" using : http://maps.googleapis.com/maps/api/geocode/json?address=nonexistingaddress%20100,C%C3%B3rdoba,C%C3%B3rdoba,Argentina&sensor=false I also get a result. I'dont know how to find out if "nonexisting" street exists or not by just analyzing the google's response. Thanks in advance, Mono.
1
votes
2 Answers
3
votes
I know this question is old, but I figured I'd answer it anyway for people still looking. The best way to handle this in my experience is to look at the API response. I don't use Google Maps for address verification, just for finding coordinates, but I need to fail properly if the address someone has provided isn't valid (at least as far as Google is concerned), since it won't return usable latitude/longitude for my app to consume.
In PHP, I do something like this:
public static function latlong($location) {
if ($location!='') {
try {
$json = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($location));
$parsedjson = json_decode($json, true);
if (key_exists(0,$parsedjson['results'])) {
$lat_long_array = $parsedjson['results'][0]['geometry']['location'];
return $lat_long_array;
} else {
return false;
}
} catch (Exception $e) {
//echo 'Caught exception: ', $e->getMessage(), "\n";
return false;
}
}
}
Hope that helps any Googlers who end up here.
2
votes
The Google Maps API v3 geocoder is not an address validator. Its purpose is to find the best coordinates for a given (postal) address.
That doesn't stop people from trying to use it