Extraiga todos los campos del resultado XML utilizando la API de codificación geográfica de Google

I am using the Google Geocoding API, specifically relying on the XML output format. The code below loops through the array $arrGeocodeSales and for each row it retrieves the $lat y $long, which are then displayed in the table.

This code is working fine, but I would like to extend it so that I can extract more fields from the $xml result that is returned. For example, some of the additional fields that I would like to extract from the $xml result are formatted_address y postal_code. How can I modify the code below to extract the additional fields?

You can see an example of an $xml result here:

http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true

Here is the code that needs to be modified. I guess I don't understand how to work with the SimpleXMLElement that is returned. Thanks for the help.

<table border="1" style="margin-bottom:0px;">
<tr>
  <th>ID</th>
  <th>Roll</th>
  <th>Address</th>
  <th>Lat</th>
  <th>Long</th>
  <th>MapSaleNumber</th>
  </tr>

<?php foreach ($arrGeocodeSales as $key => $value): 
    $address = $value['Address']."+".$value['Municipality']."+Ontario+Canada"; 
    $googleAddress = "https://maps.google.com/maps/geo?q=".urlencode($address)."&output=xml";
    // Retrieve the URL contents
    $googlePage = file_get_contents($googleAddress);
    // Parse the returned XML file
    $xml = new SimpleXMLElement($googlePage);
    // Parse the coordinate string  
    list($lng, $lat, $alt) = explode(",", $xml->Response->Placemark->Point->coordinates);
 ?>
<tr>
  <td ><?php echo str_pad($row++, 2, '0', STR_PAD_LEFT); ?></td>
  <td ><?php echo $value['SingleRoll']; ?></td>
  <td ><?php echo $address; ?> </td>
  <td ><?php echo $lat ?></td>
  <td ><?php echo $lng ?></td>
  <td ><?php echo $value['MapSaleNumber']; ?></td>
  </tr>
<?php endforeach; ?>

preguntado el 02 de mayo de 12 a las 19:05

1 Respuestas

Puedes manipular el SimpleXMLElement result using the methods documentado aquí.

You will need to iterate through the address_component elements to find one with the type element value you are interested in. Look for examples of other code using that API and you should be able to quickly figure it out.

contestado el 02 de mayo de 12 a las 19:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.