Overlaying Route Resizes the Viewport

592 views
Skip to first unread message

Roger Hampson

unread,
Aug 5, 2011, 6:25:06 AM8/5/11
to google-map...@googlegroups.com
I have created a map of the UK at zoom level 6 that fits my content page. I have overlayed a route on it which is defined by start and end coords. When the route is drawn, it resizes the map to a much smaller level. Is there any way I can make it keep the zoom level of the original map?

Rossko

unread,
Aug 5, 2011, 4:02:00 PM8/5/11
to Google Maps JavaScript API v3
Probably. But we've no idea what you use to draw your route, to
advise how to it differently.
http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/2b3f101fd509919e

geoco...@gmail.com

unread,
Aug 5, 2011, 4:45:44 PM8/5/11
to Google Maps JavaScript API v3
http://code.google.com/apis/maps/documentation/javascript/reference.html#DirectionsRendererOptions

preserveViewport

+ By default, the input map is centered and zoomed to the bounding box
of this set of directions.
+ If this option is set to true, the viewport is left unchanged,
unless the map's center and zoom
+ were never set.

-- Larry

Roger Hampson

unread,
Aug 7, 2011, 3:49:55 AM8/7/11
to google-map...@googlegroups.com
This is the code I am using to draw the map

var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var startlatlng = new google.maps.LatLng(50.067, -5.717);
var endlatlng = new google.maps.LatLng(58.633, -3.067);
var map;

function init() {
    directionsDisplay =
new google.maps.DirectionsRenderer();
   
var latlng = new google.maps.LatLng(54.6, -4.25);
   
var myOptions = { zoom: 6, center: latlng, disableDefaultUI: true, mapTypeId: google.maps.MapTypeId.ROADMAP };
   
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    directionsDisplay.setMap(map);
    calcRoute();
}

function calcRoute() {
   
var request = {
        origin: startlatlng,
        destination: endlatlng,
        avoidHighways:
true,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };

    directionsService.route(request,
function (response, status) {
       
if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
        }
    });
}

google.maps.event.addDomListener(window, 'load', init);

MymsMan

unread,
Aug 7, 2011, 5:57:58 AM8/7/11
to google-map...@googlegroups.com
You don't appear to have incorporate geoco's suggestion

var rendOpts= {preserveViewport: true}
directionsDisplay = new google.maps.DirectionsRenderer(rendOpts);

Roger Hampson

unread,
Aug 8, 2011, 3:41:36 AM8/8/11
to google-map...@googlegroups.com
I assumed, obviously wrongly, that directionsDisplay.setMap(map); would indicate that I wanted it drawn on the map created by init(). I have now included your piece of code and it is giving me what I want.
 
Thank you

MymsMan

unread,
Aug 8, 2011, 11:05:23 AM8/8/11
to google-map...@googlegroups.com
directionsDisplay.setMap(map) does tell it to render the route on the map indicated (you might have several maps on your webpage) but by default the renderer resizes and recentres the map to suit the route - you have to explicitly tell it not do that with the preserveViewport option.


Roger Hampson

unread,
Aug 9, 2011, 7:27:15 AM8/9/11
to google-map...@googlegroups.com
It seems perverse to me that the map I wanted it drawn on isn't the default. Why specify everything I want and then have a subsidiary line ignore it all.

geoco...@gmail.com

unread,
Aug 9, 2011, 10:15:09 AM8/9/11
to Google Maps JavaScript API v3
On Aug 9, 4:27 am, Roger Hampson <roger.hamp...@talktalk.net> wrote:
> It seems perverse to me that the map I wanted it drawn on isn't the default.
> Why specify everything I want and then have a subsidiary line ignore it all.

Most people, most of the time, want the map to zoom and center to show
the results of the directions query.


-- Larry
Reply all
Reply to author
Forward
0 new messages