Contacts

XHTML strict compliant Google Map embedding - with JQuery!

Google Maps

 

It seems that every time I go to embed a Google map into a site I'm working on, I forget about Google's IFRAME failure. In this particular case, I wasn't up for running through the API, and I just wanted to embed the map on the page, and have it validate. First, let me say you need to be using JQuery (if you're not, you should be). Second, a document.write is not the solution to your issue, if you're not putting the Javascript code in a separate file. As this code is only run on one page, I'm not going to bother. First, you're going to create your div wherer you'd like your map:


  1. <div id="#map"></div>

Next, you're going to wrap your JQuery calls in script tags, and escape the CDATA tags with:

  1. <script type="text/javascript">
  2. //<![CDATA[
  3. $(document).ready(function() { $("#map").append('<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;amp;source=s_q&amp;amp;hl=en&amp;amp;geocode=&amp;amp;q=Google,+Mountain+View,+CA&amp;amp;sll=39.774769,-72.949219&amp;amp;sspn=56.795811,135.351563&amp;amp;ie=UTF8&amp;amp;ll=37.432477,-122.079906&amp;amp;spn=0.029142,0.06609&amp;amp;z=14&amp;amp;iwloc=A&amp;amp;cid=11972054812773967638&amp;amp;output=embed"></iframe>
  4. <br /><small><a href="http://maps.google.com/maps?f=q&amp;amp;source=embed&amp;amp;hl=en&amp;amp;geocode=&amp;amp;q=Google,+Mountain+View,+CA&amp;amp;sll=39.774769,-72.949219&amp;amp;sspn=56.795811,135.351563&amp;amp;ie=UTF8&amp;amp;ll=37.432477,-122.079906&amp;amp;spn=0.029142,0.06609&amp;amp;z=14&amp;amp;iwloc=A&amp;amp;cid=11972054812773967638" style="color:#0000FF;text-align:left">View Larger Map</a></small>'); });
  5. //]]>
  6. </script>

This will place the map inside your div, AND validate!

Filed under: