Set Initial View When Opening KMZ in GE

6k Views Asked by At

I am creating a KMZ file as output from my program (using SharpKML). How can I set the initial view so that when this file is opened in Google Earth it centres and zooms around my data?

When I open a KML file in GE this behaviour is by default, but when I open my KMZ the browser just opens to the default whole world view centred on users country. If I double click the main KML within my KMZ file, then I get the correct zoom and centering around my data. How can I make this happen automatically when Google Earth loads the KMZ?

Thanks.

3

There are 3 best solutions below

0
On

I used to open my kmz files in Javascript and they would center automatically, but now as of a few weeks ago, my maps open up above the north pole. I created the kmz files so long ago I don't want to look at them or edit them. Is there some code I can add so that it works like it did before? I had before my open code

var overlay = new GGeoXml(\"http://www.mysite.com/data/file.kmz" ) ;
map.addOverlay( overlay ) ;

this bogus looking code that does not seem to initialze anything, but worked

var bounds = new GLatLngBounds();
// initialize bounds ??

var SWcorner= new GLatLng(parseFloat(87),parseFloat(-1));
var NEcorner= new GLatLng(parseFloat(89),parseFloat(1));
bounds.extend(SWcorner);
bounds.extend(NEcorner);
map.setCenter(new GLatLng(88.0, 0), map.getBoundsZoomLevel(bounds), 
                                        G_PHYSICAL_MAP);
0
On

There is also a really easy way to do this in Google Earth/Google Earth pro:

  1. Simply right click and bring up properties of the Folder in Places.

  2. Go to the view tab and click the Snapshot current view.

  3. Then save place as to update the kmz/kml.

This updates the <LookAt> at the level you click on- so for a point at the Placemark level or you can do it at the folder level.

1
On

If you add a LookAt or Camera to the first element in your root KML file (first .kml file within your KMZ) Google Earth will start at that location when that file is loaded.

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
<Placemark>
    <name>Google office</name>
    <LookAt>
        <longitude>-122.087387</longitude>
        <latitude>37.422130</latitude>
        <altitude>0</altitude>
        <heading>-0.23</heading>
        <tilt>47.81</tilt>
        <range>115.5</range>
        <gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
    </LookAt>
    <Point>
        <coordinates>-122.087461,37.422069</coordinates>
    </Point>
</Placemark>
</kml>

If you have multiple features then add the <LookAt> to the root-level Document.

 <kml>
   <Document>
    <LookAt>
      ...
    </LookAt>
    <Placemark>
    </Placemark>
      ...