phpgoogle-mapsgeocoding

Google Geocoder API Output Format


I noticed that my google geocoding has been deprecated and has brought my website to its knees. I could really use someone's help figuring out this last piece of the puzzle. I had the old code that produced a single output "$Coords" that the rest of my website is dependent upon. Could you please help me figure out how to make the new api have the same output. Here was my old code:

$address = str_replace('#', '', $address);
$address = str_replace(' ', '+', $address);
$XMLUrl = 'http://maps.google.com/maps/geo?q='.$address.'&key='.$api.'&sensor=false&output=xml&oe=utf8';
$XMLUrl = 'http://maps.googleapis.com/maps/api/geocode/xml?address='.$address.'&sensor=false';
$XMLContents = file_get_contents($XMLUrl);
$XML = new SimpleXMLElement($XMLContents);
$Coords = explode(',',$XML->Response->Placemark->Point->coordinates);
return $Coords;

Here is what I have for the new code. I just can't figure out how to have it output as $Coords:

$address = str_replace('#', '', $address);
$address = str_replace(" ", "+", $address);
$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
$json = json_decode($json);
$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};

The rest of the website depends upon the geocoding stored as $Coords and not as $lat or $long. I really really appreciate any help I can get.


Solution

  • I needed to save it as an array. Duh

    $Coords = $long.', '.$lat;
    $Coords = explode(',',$Coords);