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 and $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 and postal_code. How can I modify the code below to extract the additional fields?
You can see an example of an $xml result here:
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; ?>
You can manipulate the
SimpleXMLElementresult using the methods documented here.You will need to iterate through the
address_componentelements to find one with thetypeelement value you are interested in. Look for examples of other code using that API and you should be able to quickly figure it out.