我正在尝试开发一个 HTML 页面,通过 Google 地图 API 显示 KML 文件。
页面链接:http://www.slocleanair.org/air/AQI_III/mapTest.html http://www.slocleanair.org/air/AQI_III/mapTest.html
我的问题是,当我用更新的缩放值替换文件时,缩放选项不被遵守。我认为这通常是网络的缓存问题,但 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 地图 API 问题?还是我的编码错误?
代码片段:
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>
如果您不希望地图缩放以适合 KmlLayer 内容,请将preserveViewport 选项设置为true。
工作代码片段:
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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)