Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I load a KML in google map, without changing the map center

How would I add the KML as a layer in google maps (javascript v3) without changing the map extent/zoom?

I am adding a KML which has several events all over the world, because of this google maps javascript v3 changes the map extent to a very large zoomed out view.

Here is some example code:

function loadOverlay(inc) 
{
    var overlay = new google.maps.KmlLayer(inc);
    overlay.setMap(map);
    overlayArray.push(overlay);
}

function initialize_gmap()
{
    var Rochester = new google.maps.LatLng(43.1561, -77.607); 
    var myOptions = {
    zoom: 11,
    center: Rochester,
    mapTypeId: google.maps.MapTypeId.TERRAIN
    }

    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}

//Then i initialize the map here, and load the kml
initialize_gmap();
loadOverlay("linktokml.kml");

After the kml is loaded it goes to a very large zoom to show all the locations in the kml. How would I either prevent this, or go back to my initial extent/zoom?

Thanks in advance.

like image 848
Beau Bouchard Avatar asked Jul 14 '12 20:07

Beau Bouchard


People also ask

Can I load a KML file in Google Maps?

You can import map features like lines, shapes, and places to your map from KML files, spreadsheets and other files.

How do I run a KML file in navigation?

On chrome go to https://www.google.com/earth/ • Click Launch Earth in Chrome. Click the Menu button on the left side navigation bar. Click Settings. Scroll to the bottom of the settings and where it says Enable KML file import turn it on then click save.


1 Answers

Just set preserveViewport option of google.maps.KmlLayerOptions to true:

var overlay = new google.maps.KmlLayer(inc,{preserveViewport:true});
like image 180
Engineer Avatar answered Sep 20 '22 05:09

Engineer