google-maps - Google map javascript API - 应用 KML 层后 map 对象重新居中。我该如何防止这种情况?

标签 google-maps google-maps-api-3

Google map javascript API 问题 - 应用 KML 图层后 map 对象重新回到中心位置。我该如何防止这种情况发生?

我创建了一个带有 map 的简单页面。当我应用简单的多边形 KML 时,它会居中并缩放 map 。有什么想法吗?

    var myOptions = {
            center: new google.maps.LatLng(27, -97),
            zoom: 17,
            mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),            myOptions);
    var buildings = new google.maps.KmlLayer('https://mysite/site/buildings.xml');
    buildings.setMap(map);


    //i tried this to recenter and zoom, but no dice.
    var posn = new google.maps.LatLng(27, -97);
    map.setCenter(posn);
    map.setZoom(17);

最佳答案

听起来您需要 preserveViewport option 。来自文档:

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.

关于google-maps - Google map javascript API - 应用 KML 层后 map 对象重新居中。我该如何防止这种情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9385459/

相关文章:

google-maps-api-3 - 更改谷歌地图折线上符号的默认值

node.js - 是否可以在 node.js 服务器端使用 google.maps api 库?

javascript - Google map 上的多个标记

iPhone map 套件集群精确定位

android - 尝试在 Google Map V2 上放置标记时出现 NullPointerException

javascript - Google map 在 JQuery Mobile 中显示不正确

javascript - 使用 php 脚本以更快的方式获取 lat lng 值

jquery - 在 Google map 上启用拖放的准确标记位置

javascript - 如何选择特定的街景全景日期?

Android Google map v2 - 将标记保持在 map 中心