Google Maps API help

OK, I am trying to set up a page that a user can enter in an address and that information will then “generate” a call to the google maps API to make an iframe for display in another page.

If found the following online but it does not get any information

<?php

// First, setup the variables you will use on your <iframe> code
// Your Iframe will need a Width and Height set
// as well as the address you plan to Iframe
// Don't forget to get a Google Maps API key

$latitude = '';
$longitude = '';
$iframe_width = '400px';
$iframe_height = '400px';
$address = '12215 East Sprague Avenue, Spokane Valley, WA 99206';
echo 'Addres to find: ' . $address . '<br>';
$address = urlencode($address);
$key = "AIzaSyDwDLLJCtmZCJaquWBWlKecSy46i_5StGc";
$url = "http://maps.google.com/maps/geo?q=".$address."&output=json&key=".$key;
echo 'URL: ' . $url . '<br>';
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
// Comment out the line below if you receive an error on certain hosts that have security restrictions
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$data = curl_exec($ch);
curl_close($ch);

$geo_json = json_decode($data, true);

// Uncomment the line below to see the full output from the API request
// var_dump($geo_json);

// If the Json request was successful (status 200) proceed
if ($geo_json['Status']['code'] == '200') {

$latitude = $geo_json['Placemark'][0]['Point']['coordinates'][0];
$longitude = $geo_json['Placemark'][0]['Point']['coordinates'][1]; ?>

<iframe width="<?php echo $iframe_width; ?>" height="<?php echo $iframe_height; ?>" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="

http://maps.google.com/maps

?f=q
&amp;source=s_q
&amp;hl=en
&amp;geocode=
&amp;q=<?php echo $address; ?>
&amp;aq=0
&amp;ie=UTF8
&amp;hq=
&amp;hnear=<?php echo $address; ?>
&amp;t=m
&amp;ll=<?php echo $longitude; ?>,<?php echo $latitude; ?>
&amp;z=12
&amp;iwloc=
&amp;output=embed"></iframe>

<?php

} else { echo "<p>No Address Available</p>";}

?>


Putting this on my site, I get the following information:

Addres to find: 12215 East Sprague Avenue, Spokane Valley, WA 99206
URL: http://maps.google.com/maps/geo?q=12215+East+Sprague+Avenue%2C+Spokane+Valley%2C+WA+99206&output=json&key=AIzaSyDwDLLJCtmZCJaquWBWlKecSy46i_5StGc

No Address Available

Any idea why the “No Address Available” comes back?

E

Hi,

I tried clicking the link that your code generated, and I got a message saying that Google couldn’t process the request right now. I tried removing the API key from the URL, and then I got a response saying that v2 of the Google Maps API was discontinued last year. You’ll need to alter the code to work with v3 of the API.

There’s a guide for moving from v2 to v3, but it’s aimed at Javascript users, so I’m not sure how helpful it’s going to be.

Thanks but want this work with PHP - not a fan of javascript since I got hijacked via a javascript once.

Hope someone else can tell me how/what to modify to use v3 and not the v2 stuff. I found some information about v3 but was very confused as to how to change this.

E

I’ve managed to modify the script to work with v3 of the API. It no longer needs an API key to work:


<?php

$iframe_width = '400px';
$iframe_height = '400px';
$address = '12215 East Sprague Avenue, Spokane Valley, WA 99206';

echo 'Address to find: ' . $address . '<br>';

$address = urlencode($address);
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=$address&sensor=false";

echo 'URL: ' . $url . '<br>';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$data = curl_exec($ch);
curl_close($ch);

$geo_json = json_decode($data, true);

if ($geo_json['status'] == 'OK') {

	$latitude = $geo_json['results']['geometry']['location']['lat'];
	$longitude = $geo_json['results']['geometry']['location']['lng'];

	$map_params = array(
		'f' => 'q',
		'source' => 's_q',
		'hl' => 'en',
		'q' => $address,
		'aq' => 0,
		'ie' => 'UTF8',
		'hnear' => $address,
		't' => 'm',
		'll' => $longitude .','. $latitude,
		'z' => 12,
		'output' => 'embed'
	);

	$url = 'http://maps.google.com/maps?' . http_build_query($map_params);
?>

<iframe width="<?php echo $iframe_width; ?>" height="<?php echo $iframe_height; ?>" frameborder="0"
scrolling="no" marginheight="0" marginwidth="0" src="<?php echo $url ?>"></iframe>

<?php

} else {
	echo "<p>No Address Available</p>";
}