Question

I have a script which takes data from html and uses it to plot routes on a map. I'm trying to get it to take all the routes and zoom to show all of them. Here's the code:

var ombounds = new google.maps.LatLngBounds(); //bounds
    for (var r=1; $("#master-master > div").length; r++)
    {
        // Add a placemark at the start of each route
        var placemark_id = "#master-" + r + " > #placemarks-data > #placemark-0";

        var markerParameters = {};
        markerParameters.index = r-1;
        markerParameters.total = $(placemark_id + " > .total").html();
        markerParameters.mapData = $(placemark_id + " > .map").html();
        markerParameters.instructionsData = $("#master-" + r + " > #snippet").html();
        markerParameters.distanceData = $("#master-" + r + " > #distance").html();
        markerParameters.durationData = $("#master-" + r + " > #duration").html();
        markerParameters.titleData = $("#master-" + r + " > #title").html();
        markerParameters.urltitleData = $("#master-" + r + " > #url-title").html();
        markerParameters.imageData = $(placemark_id + " > .image").html();

        $.OverMap.addMarker(markerParameters);

        // Draw route
        var points = new Array();
        var pointsData = [];
        var pointsData = $('#master-' + r + ' > #route-data').html();
        if (pointsData != '')
        {
            var pointsArray = JSON.parse(pointsData);
            for (var p=0; p<pointsArray.length; p++) {
                var pt = new google.maps.LatLng(pointsArray[p][0], pointsArray[p][1]);
                points.push(pt);
                ombounds.extend(pt); //bounds
            }
            $.OverMap.drawRoute(points);

        }
        else $.OverMap.drawDirections({preserveViewport:false});
    }
    (map.getBounds()); //bounds
    map.fitBounds(ombounds); //bounds

For some reason, map.fitBounds(ombounds) does nothing. The code still plots the routes, but I can't see what the bounds related functions are doing. I've added //bounds to all the lines related to the zooming - at the moment, they have no visible affect on the code's output. Anyone know where I'm going wrong?

No correct solution

OTHER TIPS

When the preserveViewport option of the directions service is set to false, each call to the DirectionsRenderer will zoom and center the map on its result. If you want fitBounds to override it, you need to set that to true.

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