Google Maps geocoding api

Hi,

I am trying to convert an address to coordinates using the following but it doesn’t work (no errors displayed):

<?php
$data = new SimpleXMLElement(file_get_contents('http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false'));

$latitude = $data->GeocodeResponse->result->geometry->location->lat;
$longitude = $data->GeocodeResponse->result->geometry->location->lng;

echo $latitude;
echo $longitude;
?>

I guess I am doing something wrong but I can’t see what.

Thanks for any ideas.

try:


var_dump($data);

Maybe you have misspelt a key …

EDIT seems not, but keep adding accessors and see where it goes wrong …

$data->GeocodeResponse and so on …

XML parsing strips off the first element of the return as the base container. $data is already pointing at the GeocodeResponse.

$latitude = $data->result->geometry->location->lat;

will get you your value.

Thanks, I noticed that shortly after posting this question. I forgot to update here.