Google Maps zoom-to-fit [SOLVED]

JavaScript and client side scripting.

Moderator: General Moderators

Google Maps zoom-to-fit [SOLVED]

Postby amh1010 » Tue Feb 09, 2010 12:07 am

After a button is clicked, addresses (which are stored in a PHP array) are displayed on an embedded Google Map on my site. I would like the embedded map to automatically zoom to fit the markers. How would I go about this? Here's the important part of my code (I can post the entire PHP file if necessary):

Syntax: [ Download ] [ Hide ]
 
<script type="text/javascript">
   
    var map = null;
    var geocoder = null;
    var latlngbounds = new GLatLngBounds(  );
 
    function initialize() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        <?php if(!isset($_POST['viewed'])){
        echo 'map.setCenter(new GLatLng(36.879621, -95.537109), 2);';}
        else
            {
            echo 'function fitMap( GMap2, points ) {
                var bounds = new GLatLngBounds();
                for (var i=0; i< points.length; i++) {
                    bounds.extend(points[i]);
                }
                map.setZoom(map.getBoundsZoomLevel(bounds));
                map.setCenter(bounds.getCenter());
            }'
;
                       
            fitMap();
            }
       
        ?>
       
       
        map.addControl(new GLargeMapControl());
        geocoder = new GClientGeocoder();
      }
    }
 



I'm trying to use this guy's code, but I can't implement it right:
Syntax: [ Download ] [ Hide ]
 
function fitMap( map, points ) {
   var bounds = new GLatLngBounds();
   for (var i=0; i< points.length; i++) {
      bounds.extend(points[i]);
   }
   map.setZoom(map.getBoundsZoomLevel(bounds));
   map.setCenter(bounds.getCenter());
}
 

From http://blog.jeromeparadis.com/archive/2007/02/08/1468.aspx


I'm fairly well-versed with PHP, but Java is very new to me. Sorry for the long post, but I'd appreciate any help.


[EDIT: SOLVED]

I fixed this using this code: http://econym.org.uk/gmap/basic14.htm

I also changed
Syntax: [ Download ] [ Hide ]
var latlngbounds = new GLatLngBounds(  );
to
Syntax: [ Download ] [ Hide ]
var bounds = new GLatLngBounds(  );
amh1010
Forum Newbie
 
Posts: 19
Joined: Sat Feb 06, 2010 2:49 pm

Return to Javascript

Who is online

Users browsing this forum: No registered users and 2 guests