SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    PHP Code:
    $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.

    PHP Code:
    $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.

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    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.

    PHP Code:
    $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.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spikeZ View Post
    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.

    PHP Code:
    $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.

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    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:

    Code:
    <kml xmlns="http://earth.google.com/kml/2.0">
    <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>
    
    
    
    </kml>
    It gets more interesting if there are more than 1 result returned so you will have to be specific about your address format!
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •