【问题标题】:Google Maps API 3 - zoom not honoredGoogle Maps API 3 - 不支持缩放
【发布时间】:2014-12-29 21:52:20
【问题描述】:

我正在尝试开发一个通过 Google Maps API 显示 KML 文件的 HTML 页面。

页面链接: http://www.slocleanair.org/air/AQI_III/mapTest.html

我的问题是,当我用更新的缩放值替换文件时,缩放选项没有得到遵守。我认为这通常是 Web 的缓存问题,但 JavaScript 应该添加一个后缀,通过创建一个唯一的 urlSuffix 来破坏缓存。

var kmlPath = "http://www.slocleanair.org/air/AQI_III/AQI_2015_xx_xx.kml";
// Add unique number to this url - as with images - to avoid caching issues during development
var urlSuffix = (new Date).getTime().toString();
var layer = new google.maps.KmlLayer(kmlPath + '?' + urlSuffix );
layer.setMap(map);

这种技术是否可以让我在不从缓存中提取先前版本的情况下引用同名文件?或者这是 Google Maps API 的问题?还是我的编码错误?

代码sn-p:

var mylocation = {
  'latitude': 35.3,
  'longitude': -120.3
};
var map;

function initialize() {
  var myLatlng = new google.maps.LatLng(mylocation.latitude, mylocation.longitude);
  var mapOptions = {
    zoom: 12,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
  // This needs to be the Full URL - not a relative URL
  var kmlPath = "http://www.slocleanair.org/air/AQI_III/AQI_2015_xx_xx.kml";
  // Add unique number to this url - as with images - to avoid caching issues during development
  var urlSuffix = (new Date).getTime().toString();
  var layer = new google.maps.KmlLayer(kmlPath + '?' + urlSuffix);
  layer.setMap(map);
}

google.maps.event.addDomListener(window, 'load', initialize);
#map_canvas {
  margin-left: auto;
  margin-right: auto;
  padding: 0;
  width: 260px;
  height: 180px;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>

【问题讨论】:

  • Zoom and Center not working when using multiple KmlLayers (Google maps) 的可能副本,如果您不希望地图缩放以适​​应 KmlLayer 内容,请将 preserveViewport 选项设置为 true。
  • 不确定您对缓存的抱怨是什么。 KML 有变化吗?
  • 我正在开发过程中更改 KML。此外,我们有时需要在中午更新地图,而重复用户需要能够看到新地图。

标签: javascript google-maps-api-3


【解决方案1】:

如果您不希望地图缩放以适​​应 KmlLayer 内容,请将 preserveViewport 选项设置为 true。

工作代码sn-p:

  var mylocation = {'latitude':  35.3,'longitude':  -120.3};
  var map;
  function initialize() {
    var myLatlng = new google.maps.LatLng( mylocation.latitude, mylocation.longitude );
    var mapOptions = {zoom: 12,center: myLatlng,mapTypeId: google.maps.MapTypeId.ROADMAP};
    var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
    // This needs to be the Full URL - not a relative URL
    var kmlPath = "http://www.slocleanair.org/air/AQI_III/AQI_2015_xx_xx.kml";
    // Add unique number to this url - as with images - to avoid caching issues during development
    var urlSuffix = (new Date).getTime().toString();
    var layer = new google.maps.KmlLayer({url:kmlPath + '?' + urlSuffix, preserveViewport: true} );
    layer.setMap(map);
  }

  google.maps.event.addDomListener(window, 'load', initialize);
#map_canvas {margin-left: auto;margin-right: auto;padding: 0;width: 260px;height: 180px;}
<script src="http://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>

【讨论】:

  • 谢谢 - 如何使地图缩放以适​​应?我需要在一个页面上显示地图两次,一次是全图,一次是放大到右下方。
  • KmlLayer 默认缩放以适应(或当 preserveViewport 选项设置为 false 时)。
猜你喜欢
  • 2022-01-22
  • 2012-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-18
  • 2011-03-15
  • 2014-04-16
相关资源
最近更新 更多