Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have just finished adding Google Maps to my company's CMS offering. My code allows for more than one map in a page.</p> <p><strong>Notes:</strong></p> <ul> <li>I use jQuery</li> <li>I put the address in the content and then parse it out to dynamically generate the map</li> <li>I include a Marker and an InfoWindow in my map</li> </ul> <p><strong>HTML:</strong></p> <pre><code>&lt;div class="block maps first"&gt; &lt;div class="content"&gt; &lt;div class="map_canvas"&gt; &lt;div class="infotext"&gt; &lt;div class="location"&gt;Middle East Bakery &amp; Grocery&lt;/div&gt; &lt;div class="address"&gt;327 5th St&lt;/div&gt; &lt;div class="city"&gt;West Palm Beach&lt;/div&gt; &lt;div class="state"&gt;FL&lt;/div&gt; &lt;div class="zip"&gt;33401-3995&lt;/div&gt; &lt;div class="country"&gt;USA&lt;/div&gt; &lt;div class="phone"&gt;(561) 659-4050&lt;/div&gt; &lt;div class="zoom"&gt;14&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="block maps last"&gt; &lt;div class="content"&gt; &lt;div class="map_canvas"&gt; &lt;div class="infotext"&gt; &lt;div class="location"&gt;Global Design, Inc&lt;/div&gt; &lt;div class="address"&gt;3434 SW Ash Pl&lt;/div&gt; &lt;div class="city"&gt;Palm City&lt;/div&gt; &lt;div class="state"&gt;FL&lt;/div&gt; &lt;div class="zip"&gt;34990&lt;/div&gt; &lt;div class="country"&gt;USA&lt;/div&gt; &lt;div class="phone"&gt;&lt;/div&gt; &lt;div class="zoom"&gt;17&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p><strong>Code:</strong></p> <pre><code>$(document).ready(function() { $maps = $('.block.maps .content .map_canvas'); $maps.each(function(index, Element) { $infotext = $(Element).children('.infotext'); var myOptions = { 'zoom': parseInt($infotext.children('.zoom').text()), 'mapTypeId': google.maps.MapTypeId.ROADMAP }; var map; var geocoder; var marker; var infowindow; var address = $infotext.children('.address').text() + ', ' + $infotext.children('.city').text() + ', ' + $infotext.children('.state').text() + ' ' + $infotext.children('.zip').text() + ', ' + $infotext.children('.country').text() ; var content = '&lt;strong&gt;' + $infotext.children('.location').text() + '&lt;/strong&gt;&lt;br /&gt;' + $infotext.children('.address').text() + '&lt;br /&gt;' + $infotext.children('.city').text() + ', ' + $infotext.children('.state').text() + ' ' + $infotext.children('.zip').text() ; if (0 &lt; $infotext.children('.phone').text().length) { content += '&lt;br /&gt;' + $infotext.children('.phone').text(); } geocoder = new google.maps.Geocoder(); geocoder.geocode({'address': address}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { myOptions.center = results[0].geometry.location; map = new google.maps.Map(Element, myOptions); marker = new google.maps.Marker({ map: map, position: results[0].geometry.location, title: $infotext.children('.location').text() }); infowindow = new google.maps.InfoWindow({'content': content}); google.maps.event.addListener(map, 'tilesloaded', function(event) { infowindow.open(map, marker); }); google.maps.event.addListener(marker, 'click', function() { infowindow.open(map, marker); }); } else { alert('The address could not be found for the following reason: ' + status); } }); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload