Static to Dynamic Google Map 0.1

Created Tuesday 4th of January 2011 by Andreas Lagerkvist
Copyright © 2011 Andreas Lagerkvist (andreaslagerkvist.com)

What it Does

This plug-in turns a static google map into a dynamic one by inspecting the attributes passed to the static map API. It's still in beta and doesn't support everything you can pass to the static map API. Note that the plug-in obviously require you include the google maps api before running it. Since I'm not doing that on my site the example is just a static map.

How to Use

jQuery(document.body).staticToDynamicGM(); would turn every static map in the document into a dynamic one.

Example

Example Code

HTML

<div id="jquery-static-to-dynamic-gm-example">
<
img src="http://maps.google.com/maps/api/staticmap?center=59.33961,18.080712&amp;zoom=15&amp;size=440x200&amp;sensor=false&amp;markers=59.33961,18.080712" alt="" />
</
div>

JS

jQuery('#jquery-static-to-dynamic-gm').staticToDynamicGM();

Source Code

(function ($) {
    $.
fn.staticToDynamicGM = function (conf) {
        
// Config
        
var config = $.extend({
            
mapURL:        'http://maps.google.com/maps/api/staticmap'
            
className:    'jquery-static-to-dynamic-gm'
        
}, conf);

        
// Make sure Google Maps API is included
        
if (typeof(google) == 'undefined' || typeof(google.maps) == 'undefined') {
            return 
this;
        }

        
// Be nice
        
return this.each(function () {
            
// Find all static google map images and turn them into JS maps
            
$(this).find('img[src^="' config.mapURL '"]').each(function () {
                var 
img        = $(this);
                var 
attrs    img.attr('src').split('?');
                var 
opts    = {
                    
zoom:        8
                    
center:        new google.maps.LatLng(-34.397150.644), 
                    
mapTypeId:    google.maps.MapTypeId.ROADMAP
                
};

                
// Make sure map has some attributes
                
if (attrs.length 1) {
                    
attrs attrs[1].split('&');

                    
// Go through each attribute
                    
for (var i in attrs) {
                        var 
attr attrs[i].split('=');

                        
// Center needs to be an instance of LatLng
                        
if (attr[0] == 'center') {
                            var 
latLng attr[1].split(',');

                            
attr[1] = new google.maps.LatLng(latLng[0], latLng[1]);
                        }
                        
// Size will be used for the size of the div
                        
else if (attr[0] == 'size') {
                            var 
widthHeight attr[1].split('x');

                            
attr[1] = {
                                
width:    widthHeight[0], 
                                
height:    widthHeight[1]
                            };
                        }
                        
// Convert zoom level to int
                        
else if (attr[0] == 'zoom') {
                            
attr[1] = parseInt(attr[1], 10);
                        }
                        
// Markers need special treatment
                        
else if (attr[0] == 'markers') {
                            var 
markers attr[1].split('|');

                            
attr[1] = [];

                            for (var 
j in markers) {
                                var 
markerLatLng markers[j].split(',');

                                
attr[1].push(new google.maps.LatLng(markerLatLng[0], markerLatLng[1]));
                            }
                        }

                        
opts[attr[0]] = attr[1];
                    }

                    
// Now we have all the data we need to create the map
                    
var mapDiv    = $('<div class="' config.className '" style="width: ' opts.size.width 'px; height: ' opts.size.height 'px;"></div>').insertAfter(img.hide());
                    var 
map        = new google.maps.Map(mapDiv[0], opts);

                    
// Now place potential markers
                    
if (typeof(opts.markers) != 'undefined') {
                        for (var 
i in opts.markers) {
                            new 
google.maps.Marker({
                                
position:    opts.markers[j], 
                                
map:        map
                            
});
                        }
                    }
                }
            });
        });
    };
})(
jQuery);

Download

Plug-in

Requires

blog comments powered by Disqus

Random jQuery Plug-ins

  • Ajax Loader

    Use this plug-in when you want to inform your visitors that a certain part of your page is currently loading. The plug-in adds a faded 'loading-div' o...

    Details

  • Equal Height

    This plug-in makes HTML-elements equal height by adjusting their min-height CSS properties. Of course IE6 has no clue what min-height means so the plu...

    Details

  • Checked checkbox-parent

    This tiny plug-in adds and removes a 'checked'-class to input[type=checkbox]'s parents. Very useful if you want to style the entire wrapping <label>-e...

    Details

More Plug-ins

Recent Comments

Powered by Disqus
Page cached. Loaded in: 0.0187 second(s).
Last DB change: 2012-04-02 11:06:05
Last file change: 2012-04-25 20:30:39
Cache created: 2012-05-17 08:51:06