简体   繁体   中英

Google maps API3 center map

I'm using Google Maps Api3 to show my KML markers. I've set the LatLng to center and zoom on a location, but the map seems to automatic zoom out to show all the markers (I don't want that)

Here's my javascript:

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/jskey=XXX&sensor=false"></script>

<script type=text/javascript>
function initialize() {
  var foo = new google.maps.LatLng(58.404590,-15.743408);
  var mapOptions = {
    zoom: 11,
    center: foo,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
  var ctaLayer = new google.maps.KmlLayer({
    url: 'mykml.kml'
  });
  ctaLayer.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);</script>

Any ideas?

Add preserveViewport: true to your KMLLayerOptions:

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

https://developers.google.com/maps/documentation/javascript/reference#KmlLayerOptions

var ctaLayer = new google.maps.KmlLayer({
    url: 'mykml.kml',
    preserveViewport: true
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM