load kml, centre upon it but no zoom?

610 views
Skip to first unread message

PaulG

unread,
Jul 2, 2010, 9:58:09 AM7/2/10
to Google Maps JavaScript API v3
I have a number of features in separate kml files, all contained
within a pretty tight area.

I can load them up dynamically and this works fine, think of a road
gazetteer.

However I don't want the viewport zooming in and out for these
particular features.

I want the current viewport to pan to the centre point of the kml
file, but stay at the current zoom level.

I am supressing the normal map.kmlLayer() with

{preserveViewport:true}

so do I have any way of finding the centre point of that kmlLayer?

The only other way I can think of doing this is to

-remember the current zoom level
-turn preserveViewport back to false
-ZOOM in when the kml load
-then ZOOM back out again

Which might be a bit disconcerting for users.

Nathan Raley

unread,
Jul 2, 2010, 10:03:42 AM7/2/10
to google-map...@googlegroups.com
getDefaultViewport() Gets the default viewport for the layer being displayed and returns a LatLngBounds of the Layer you are displaying.  LatLngBounds has a getCenter.  You could read the current zoom and read the getDefaultViewport and set the map to center on the viewports center and the previous zoom level.


--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To post to this group, send email to google-map...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.


PaulG

unread,
Jul 2, 2010, 12:11:07 PM7/2/10
to Google Maps JavaScript API v3
Well that was a fun-filled few hours .. but I got it working thanks to
your advice and info I found in other threads.

http://groups.google.co.uk/group/google-maps-js-api-v3/browse_thread/thread/ee2479ef82638d8b

http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/b73a280d9d63a981

Example, a function to load some roads from kml files:

where map is a global var, a gmap3 element.

function showRoad( file ){

var road = new google.maps.KmlLayer(
'http://www.yourwebsite.com/' + file + '.kml',
{preserveViewport:true}
);

road.setMap(map);
google.maps.event.addListenerOnce(road, "defaultviewport_changed",
function() {
recenter = road.getDefaultViewport().getCenter() ;
map.panTo( recenter ) ;
});
}

The main learning point for me was that you have to wait for the
defaultviewport to change before trying to access the layer, and you
can chain methods in JS, didn't know that.

Thanks for your help all.

Nathan Raley

unread,
Jul 2, 2010, 12:16:10 PM7/2/10
to google-map...@googlegroups.com
Glad I could be of some help and that you were able to find everything you needed to get it done.


--
Reply all
Reply to author
Forward
0 new messages