preserveViewport and KML files

3,531 views
Skip to first unread message

Joseph Elfelt

unread,
Jul 9, 2010, 3:46:45 PM7/9/10
to Google Maps JavaScript API v3
Alas, I have not been able to figure out how to do these two things
with KML files:

A. When the map first appears on the user's screen, then display a
KML file and let the API automatically set the map center and zoom
B. As the user toggles the KML on/off, preserve the viewport

Below is Google's KML demo map to which I added
preserveViewport:true
Adding this setting is the key to doing item 'B'.

However, the initial appearance of the map (item 'A') uses the zoom
setting in the options and as a result only part of the KML data is on
the map. When I tried deleting the zoom setting in the options, then
I got a blank map.

http://www.mappingsupport.com/forum_post/preserveViewport_kml_10.html

I know how to toggle the KML on/off.

Any ideas for how I can get all the KML to appear on the map when it
first appears on the screen?

Chad Killingsworth

unread,
Jul 9, 2010, 4:51:28 PM7/9/10
to Google Maps JavaScript API v3
preserveViewport: true tells the API NOT to adjust the map center and
zoom level when loading a KML file. I believe you want
preserveViewport: false.

Chad Killingsworth

geoco...@gmail.com

unread,
Jul 9, 2010, 5:19:21 PM7/9/10
to Google Maps JavaScript API v3
On Jul 9, 3:46 pm, Joseph Elfelt <josephelf...@gmail.com> wrote:
> Alas, I have not been able to figure out how to do these two things
> with KML files:
>
> A.  When the map first appears on the user's screen, then display a
> KML file and let the API automatically set the map center and zoom

This seems to work for one kml file:
google.maps.event.addListener(ctaLayer, "metadata_changed" ,
function() {
map.fitBounds(ctaLayer.getDefaultViewport());
});

Since you haven't provided a link to your map, I don't know if it will
work for your code structure and I don't know that the
"metadata_changed" event is the best way to tell that the kmllayer has
loaded, I don't see a complete list of the available events in the
documentation. Maybe one of the google engineers can shed some light
on where those would be documented.

> B.  As the user toggles the KML on/off, preserve the viewport
>
> Below is Google's KML demo map to which I added
> preserveViewport:true
> Adding this setting is the key to doing item 'B'.
>
> However, the initial appearance of the map (item 'A') uses the zoom
> setting in the options and as a result only part of the KML data is on
> the map.  When I  tried deleting the zoom setting in the options, then
> I got a blank map.

I believe the initial zoom is needed to "initialize" the map.
Although the documentation isn't very clear about what is required to
initialize the map.

-- Larry

Joseph Elfelt

unread,
Jul 9, 2010, 5:48:40 PM7/9/10
to Google Maps JavaScript API v3
Chad,

I apologize for not doing a better job explaining.
Here are 2 demo maps. They both have a custom control the user can
click to toggle the kml.

The app I am working on never knows what kml file it is going to be
asked to display.
Therefore you will see in the map options I have arbitrarily set the
center to 0,0 and zoom to 5

This map has preserveViewport:false
http://www.mappingsupport.com/forum_post/preserveViewport_kml_25.html
1. It correctly shows the kml data when the map first appears. Good.
2. Now click the control to turn the kml off and change the viewport
by dragging the map.
Turn the kml back on and the viewport you set by dragging is ignored.
Bad

This map has preserveViewport:true
http://www.mappingsupport.com/forum_post/preserveViewport_kml_27.html
1. When the map first appears it is centered at 0,0. Bad. But if
you drag the map back to Chicago and zoom in, you do see the kml.
2. Now click the control to turn the kml off and change the viewport
by dragging the map.
Turn the kml back on and the viewport you set by dragging remains the
same. Good

I am baffled on how to get 2 'Goods'.

Is there some syntax that will let me initially set
preserveViewport:false and then change it to preserveViewport:true?


Joseph Elfelt

unread,
Jul 9, 2010, 5:52:15 PM7/9/10
to Google Maps JavaScript API v3
Larry,

Thanks! I'll try that and post a map link

Chad Killingsworth

unread,
Jul 9, 2010, 5:59:00 PM7/9/10
to Google Maps JavaScript API v3
> Is there some syntax that will let me initially set
> preserveViewport:false and then change it to preserveViewport:true?

It's not expressly documented, but calling:

ctaLayer.set('preserveViewport', true);

after the initial display of the layer will give you the behavior you
want. This works because a KMLLayer is an MVCObject. I'm hoping better
documentation on these options is coming soon.

Chad Killingsworth

Joseph Elfelt

unread,
Jul 9, 2010, 6:36:35 PM7/9/10
to Google Maps JavaScript API v3
Chad,

Many thanks!
Undocumented, huh?
Well shoot - no wonder I was having probs...
I had to wrap your goodie in a couple of listeners.

The demo map below works exactly as I wanted. Yippee!
This was the last known item in upgrading a v2 app to v3.
I'll report here on the app itself after some beta testing.

http://www.mappingsupport.com/forum_post/preserveViewport_kml_35.html

Pssssstttt.....hey buddy.......got any more of those undocumented
thingies?
Reply all
Reply to author
Forward
0 new messages