Oops!  My last post on this thread was for a related but different
problem.  The following is how I ended up using PHP to read and parse
the json array returned from a Geocode web service request.

<?php
//This code builds a geocode query from an address, issues the query,
reads the json result into a variable which is then parsed into
address components and the Latitude and Longitude of that address.
$postdata = http_build_query(
    array(
        'var1' => '',
        'var2' => ''
    )
);

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-
urlencoded',
        'content' => $postdata
    )
);
$context  = stream_context_create($opts);

$Street_no = '1600 ';
$Street = ' Pennsylvania  Ave ';
$City = 'Washington  ';
$State = 'DC';
$Zip = '';
$jsonStreet_no = str_replace(" ", "+", (str_replace("  ", " ",
trim($Street_no)))); //Replace spaces with +
$jsonStreet = str_replace(" ", "+", (str_replace("  ", " ",
trim($Street))));
$jsonCity = str_replace(" ", "+", (str_replace("  ", " ",
trim($City))));
$jsonState = str_replace(" ", "+", (str_replace("  ", " ",
trim($State))));
$jsonZip = str_replace(" ", "+", (str_replace("  ", " ",
trim($Zip))));

$address_string = $jsonStreet_no . '+' . $jsonStreet . ',+' .
$jsonCity . ',+' . $jsonState . ',+' . $jsonZip;
$geocode_string = "http://maps.google.com/maps/api/geocode/json?
address=" . $address_string . "&sensor=false";
print 'geocode_string: ' . $geocode_string;
$result = file_get_contents($geocode_string, false, $context);

?>
<?php
echo "<br>";
?>
<?php
//echo 'Results: ' . $result;
echo "<br>";
?>

<?php
print '<u><b>Here are the parsed results:</B></u>';
echo "<br>";
$json_a = json_decode($result, true); //The 'true' parameter returns
an array.  W/O it an object is returned so use -> to deconstruct the
object
print 'Status: ' . $json_a['status'];
echo "<br>";
print 'Results formatted_address: ' . $json_a['results'][0]
['formatted_address'];
echo "<br>";
print 'Results address_StreetNO: ' . $json_a['results'][0]
['address_components'][0]['long_name'];
echo "<br>";
print 'Results address_Street: ' . $json_a['results'][0]
['address_components'][1]['long_name'];
echo "<br>";
print 'Results address_City: ' . $json_a['results'][0]
['address_components'][2]['long_name'];
echo "<br>";
print 'Results address_State: ' . $json_a['results'][0]
['address_components'][5]['short_name'];
echo "<br>";
print 'Results address_Zip: ' . $json_a['results'][0]
['address_components'][7]['long_name'];
echo "<br>";
print 'Results address_County: ' . $json_a['results'][0]
['address_components'][4]['long_name'];
echo "<br>";
print 'Results geometry Latitude: ' . $json_a['results'][0]['geometry']
['location']['lat'];
echo "<br>";
print 'Results geometry Longitude: ' . $json_a['results'][0]
['geometry']['location']['lng'];
echo "<br>";
print 'Results geometry location_type: ' . $json_a['results'][0]
['geometry']['location_type'];
echo "<br>";
$coords = $json_a['results'][0]['geometry']['location']['lat'] . ',' .
$json_a['results'][0]['geometry']['location']['lng'];
print $coords;
echo "<br><br>";
?>

-- 
You received this message because you are subscribed to the Google Groups 
"Google Maps JavaScript API v3" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-maps-js-api-v3?hl=en.

Reply via email to