Question

I'm using this jQuery plugin for my Google Map.

I have a small form that I use to filter my markers on the map.

Markers are created using $('#map_canvas').gmap('addMarker', {...}) function of the plugin, and one of the properties I can pass when creating a marker with that function is bounds:true. What this does is - when all the markers are created and displayed the map should zoom into the boundary of all the markers on the map.

Problem is - for some reason when I filter( press Submit button ), the map doesn't zoom in to the bounds of the new markers. It just stays where it was.

Why is that? How do I make it zoom in to the bounds of the new markers?

jQuery:

// when map is initialized, plot all the markers    
$('#map_canvas').gmap(mapOptions).bind('init', function(){
                        $.getJSON('myscript.php', function(json){
                            var theMarkers = json;
                            $.each(theMarkers, function(i, val) {
                                $('#map_canvas').gmap('addMarker', { 'position': new google.maps.LatLng(val.Lat, val.Lng), 'bounds':true, 'icon':'myicon.png' } ).click(function(){                             
                                    $('#map_canvas').gmap('openInfoWindow', { 'content': '<h1>'+val.Name+'</h1>'+'<h2 style="color: grey">'+val.Address+'</h2><p style="color: green">'+val.Telephone+'</p>' }, this);
                                });                     
                            });
                        }); 

                    });

                    // upon clicking submit button - clear old markers and plot newly filtered ones on the map
                    $('#myform').on('submit', function(e) {
                        $('#map_canvas').gmap('clear', 'markers');
                        e.preventDefault();
                        var myData = $('#myform').serializeArray();
                        $.getJSON('myscript.php', myData, function(json){
                            var theMarkers = json;
                            $.each(theMarkers, function(i, val) {
                                $('#map_canvas').gmap('addMarker', { 'position': new google.maps.LatLng(val.Lat, val.Lng), 'bounds':true, 'icon':'myicon.png' } ).click(function(){                             
                                    $('#map_canvas').gmap('openInfoWindow', { 'content': '<h1>'+val.Name+'</h1>'+'<h2 style="color: grey">'+val.Address+'</h2><p style="color: green">'+val.Telephone+'</p>' }, this);
                                });                     
                            });

                        }); 
                    });
Was it helpful?

Solution

$('#map_canvas').gmap('set', 'bounds', null);

Turns out you got to reset the bounds. Before plotting/creating markers.

http://code.google.com/p/jquery-ui-map/issues/detail?id=42

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top