Dynamically using Iframe Maps with PHP GeoCoding and Google Maps API

Dynamically using Iframe Maps with PHP GeoCoding and Google Maps API

Filed under News Blog, Tutorials

Written By : Kyle Bahr

In a recent project, EnlightenMental Productions needed to dynamically display a Google Map Iframe code based on the user’s current selection.  Each time an address was chosen, the <iframe> map would update to display the current address selected.

In order to do this, we needed to query the Google Maps API to get the longitude and latitude based on an address (1234 Somewhere Street City State Zipcode).  Using an article we found online (www.timshowers.com) and making a few minor adjustments, we created a basic PHP script that will generate a dynamic <iframe>.


// 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 = urlencode($address);
$url = "http://maps.google.com/maps/geo?q=".$address."&output=json&key=".$key;
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER,0);
// 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);

$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="
&amp;q=<?php echo $address; ?>
&amp;hnear=<?php echo $address; ?>
&amp;ll=<?php echo $longitude; ?>,<?php echo $latitude; ?>


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


The above code will output an Iframe into your site and use the dynamic address you’ve selected.

Written By : Kyle Bahr

With nearly ten years experience in digital media, graphic design, front-end development and creative direction, Kyle Bahr has spent the last several years growing an online presence and portfolio with EnlightenMental Productions with the hope to build continued success online and in life. A strong...

has written 9 articles

10 Responses to “Dynamically using Iframe Maps with PHP GeoCoding and Google Maps API”

  1. Cerviche says:

    Well considered and well written. I was easily able to convert this into a function that stored the iFrame src string into a database. Nice work – thanks!

  2. For those who want to encode international carachters like “ã, õ, ú” or something like that, don’t forget to utf8_encode() the address variable!!

    On Kyle’s code line 13 replace by $address = urlencode(utf8_encode($address));

    this Kyle’s code work amazingly perfect for me!

  3. Volker says:

    This functions quite good 🙂
    But when using hover effect to show the iframe as a hover “tooltip” the markup isn’t visible – Do you have an idea how to fix this?

    Btw. Great work!

  4. Taabeer says:

    Nice work Thanks!

  5. db says:

    Thanks a bundle. This works nicely on one of my servers. However, for the life of me, I cant figure out why it is not working on the live site. Same code. phpinfo shows curl is enabled. Any ideas?

  6. Nisar shaikh says:

    Nice work thanks !

    And I want to Customized Google Map- with markers for properties that are uploaded, transportation and nearby attractions.

    if possible let me know.

    Thanks in Advance.

  7. Awesome tutorial. Thanks!

    Really complex stuff -> Is there a way to show the Google Map and the Google Street View?

  8. hima says:

    when i am trying to run this, why my geo_json is returnng false in if loop? when i tried to see what exactly the var has before it runs into if loop, it echoed NULL, which is why it is failingthe if statement and jumping to the else part.

    can someone pls throw me some light on this..am relatively new to php.

  9. Gareth says:

    Thanks very much for this. I think the new API using JavaScript rather than an iframe might be better now (I realise this article was written a while ago).

    In any case this helped me out on a recent project!

  10. kanishk says:

    hi…Thanks for your tutorial…but i am getting the fatal error on line no 16 ,help me plz..

Elite Online Marketing

EnlightenMental Productions has rebranded!

We've changed our business name to Elite Online Marketing. We'll be offering the same great quality of service, but are rebranding to better showcase the new offerings we have available for our customers.

Learn More