I am trying to verify an address for Google Maps. I mean, if the address is a real address, the map will be displayed, else a default map will be displayed. I tried the following but I get a notice for non-real addresses.
$data = new SimpleXMLElement(file_get_contents('http://maps.google.com/maps/geo?output=xml&q='.$address));
if (is_object($data)) {
// address is real. display map for address.
} else {
// address is not real. display default map.
}
$data = new SimpleXMLElement(file_get_contents('http://maps.google.com/maps/geo?output=xml&q='.$address));
if ($data instanceof SimpleXMLElement) {
// address is real. display map for address.
} else {
// address is not real. display default map.
}
The Google Maps sends back an XML file with the data on it so it should always be an object.
It also sends a Status code as one of the nodes, you can use this to check if the address exists or not.
$data = new SimpleXMLElement(file_get_contents('http://maps.google.com/maps/geo?output=xml&q='.$address));
//returns and object that contains a status code
if($data->Response->Status->code == 200) {
echo 'Correct Address - Map needed';
} else
if($data->Response->Status->code == 602) {
echo 'Incorrect address - do something else';
}