0

I have created a android app that gets the gpslocation and creates a url: https://maps.googleapis.com/maps/api/geocode/xml?latlng=00.00,00.00&sensor=true

Example: https://maps.googleapis.com/maps/api/geocode/xml?latlng=42.120636,-72.568731&sensor=true

This returns (only a part):

<GeocodeResponse>
    <status>OK</status>
    <result>
        <type>street_address</type>
        <formatted_address>
            17 Dorchester Street, Springfield, Massachusetts 01109, United States
        </formatted_address>
        <address_component>
            <long_name>17</long_name>
            <short_name>17</short_name>
            <type> ...

And i'm interested in this part: 17 Dorchester Street, Springfield, Massachusetts 01109, United States.

And I would like to create a new url that contains the zip number code "01109" like http://mysite.com?process=01109 and open this site.

Can anyone help me!

dfsq
  • 191,768
  • 25
  • 236
  • 258
Mdlc
  • 7,128
  • 12
  • 55
  • 98
  • The Zip code is in a separate XML element further down the page, you can fetch it from there without having to deal with the full address. For how to parse XML, see [How to parse and process HTML/XML with PHP?](http://stackoverflow.com/q/3577641) – Pekka Mar 16 '13 at 12:06

3 Answers3

0

So, the XML you link to actually has the postal code in it. It is an address_component with a child type that has the value postal_code. The easiest way to get there, IMHO, is XPath:

$d = <<<HER
<GeocodeResponse>
  <!-- yada... --> 
  <result>
    <!-- yada --> 
    <address_component>
        <long_name>01109</long_name>
        <short_name>01109</short_name>
        <type>postal_code</type>
    </address_component>
    <!-- yada --> 
   </result>
   <!-- yoda --> 
</GeocodeResponse>
HER;

$doc = new DomDocument();
$doc->loadXML($d);
$path = new DomXPath($doc);
/*
the query translates->
   // = all nodes
   address_component = which have the type of 'address_component'
   type = the children of the address_component with the type 'type'   
   [text() = "postal_code"] = but only the type's with the value 'postal_code'
   /preceding-sibling = all nodes before this one in the same parent
   ::*[1] = the one most adjacent of the sibling
*/

$p = $path->query(
  '//address_component/type[text() = "postal_code"]/preceding-sibling::*[1]');
$newUrl = 'http://mysite.com?process='.$p->item(0)->nodeValue;
print($newUrl);
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
0

To obtain specific data from the googleapis geocode response you not only need to locate an element by it's name but also by it's content.

This can be easily done with Xpath. Luckily the SimpleXML extension in PHP has good support for reading such common formatted XML documents.

First for the Xpath:

<GeocodeResponse>
    <result>
            <type>street_address</type>

The <result> element is the element that has a child <type> which node-value is street_address. In Xpath this is expressed as:

/*/result/type[. = "street_address"]/..

And it works similar for the ZIP-code. It is the <address_component> child of the previous <result> node that has a child <type> which node-value is postal_code and of that element you want:

address_component/type[. = "postal_code"]/..

So far for the xpaths. All you need to get this to run is to load the XML document, execute the paths and read out the values you're interested in:

$xml = simplexml_load_file($xmlFile);

list($result) = $xml->xpath('/*/result/type[. = "street_address"]/..');
list($postal_code) = $result->xpath('address_component/type[. = "postal_code"]/..');

echo $result->formatted_address, "\nZIP: ", $postal_code->long_name, "\n";

With the XML documented linked in your question this creates the following output:

17 Dorchester Street, Springfield, MA 01109, USA
ZIP: 01109

I hope this is helpful.

hakre
  • 193,403
  • 52
  • 435
  • 836
0

You can access the postal_code short_name using simplexml and xpath. Here's a working example:

$location = simplexml_load_file('https://maps.googleapis.com/maps/api/geocode/xml?latlng=42.120636,-72.568731&sensor=true');

$postal_code_search = 
$location->xpath('//result[type="street_address"]/address_component[type="postal_code"]/short_name');
$postal_code = (string) $postal_code_search[0];

Notice that you must cast the values to string explicitly, as simplexml will return array or object to variables versus a string when printed (which can be confusing when testing).

Anthony
  • 36,459
  • 25
  • 97
  • 163