"Notice: Trying to get property of non-object..."

Hi,

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.
}

Thanks for any ideas.

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';
}

Try that.

Thank you very much, didn’t know about the status code. It worked.

No problem, the returned xml has many nodes that could be useful to you.
Here is the full node structure from a search for Glossop:



[COLOR=#000000][FONT=monospace]<kml xmlns="http://earth.google.com/kml/2.0">[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]<Response>
<name>glossop</name>
<Status>
<code>200</code>
<request>geocode</request>

</Status>


<Placemark id="p1">
<address>Glossop, Derbyshire, UK</address>
<AddressDetails xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0" Accuracy="4">
<Country>
<CountryNameCode>GB</CountryNameCode>
<CountryName>UK</CountryName>
<AdministrativeArea>
<AdministrativeAreaName>Derbyshire</AdministrativeAreaName>
<Locality>
<LocalityName>Glossop</LocalityName>

</Locality>



</AdministrativeArea>



</Country>



</AddressDetails>


<ExtendedData>
<LatLonBox north="53.4575653" south="53.4280995" east="-1.9149820" west="-1.9952815"/>

</ExtendedData>


<Point>
<coordinates>-1.9489070,53.4433284,0</coordinates>

</Point>



</Placemark>



</Response>


[/FONT][/COLOR]
[COLOR=#000000][FONT=monospace]</kml>[/FONT][/COLOR]

It gets more interesting if there are more than 1 result returned so you will have to be specific about your address format!