javascript - 嵌入式谷歌地图未在 Firefox 中完全呈现
问题描述
我最近使用 Places Autocomplete 小部件设置了一个嵌入式谷歌地图,并注意到在 Firefox 中,UI 呈现一半时间或根本不呈现。有时我会得到一张空白的“灰色死亡地图”。该地图在 Chrome 上运行良好,性能问题为零。这是一个已知的问题?
我的代码基于 Google 文档中的 Places Autocomplete 示例。下面是 Maps js 开发人员文档中保存的小提琴。当我在 Firefox 中打开它时,它会延迟或不完整地呈现。如果渲染不完整,并且我触发了搜索事件,则 UI 将完全加载。
https://jsfiddle.net/rcatwr/osza564q/
对此类问题的大多数回复建议刷新地图(google.maps.event.trigger(map, 'resize');)或确保正确设置地图选项(缩放和居中)。我研究了这些路径。就像我说的,它在 Chrome 中效果很好,在 Firefox 中效果不佳。
// This example requires the Places library. Include the libraries=places
// parameter when you first load the API. For example:
// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -33.8688, lng: 151.2195},
zoom: 13
});
var card = document.getElementById('pac-card');
var input = document.getElementById('pac-input');
var types = document.getElementById('type-selector');
var strictBounds = document.getElementById('strict-bounds-selector');
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(card);
var autocomplete = new google.maps.places.Autocomplete(input);
// Bind the map's bounds (viewport) property to the autocomplete object,
// so that the autocomplete requests use the current map bounds for the
// bounds option in the request.
autocomplete.bindTo('bounds', map);
// Set the data fields to return when the user selects a place.
autocomplete.setFields(
['address_components', 'geometry', 'icon', 'name']);
var infowindow = new google.maps.InfoWindow();
var infowindowContent = document.getElementById('infowindow-content');
infowindow.setContent(infowindowContent);
var marker = new google.maps.Marker({
map: map,
anchorPoint: new google.maps.Point(0, -29)
});
autocomplete.addListener('place_changed', function() {
infowindow.close();
marker.setVisible(false);
var place = autocomplete.getPlace();
if (!place.geometry) {
// User entered the name of a Place that was not suggested and
// pressed the Enter key, or the Place Details request failed.
window.alert("No details available for input: '" + place.name + "'");
return;
}
// If the place has a geometry, then present it on a map.
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(17); // Why 17? Because it looks good.
}
marker.setPosition(place.geometry.location);
marker.setVisible(true);
var address = '';
if (place.address_components) {
address = [
(place.address_components[0] && place.address_components[0].short_name || ''),
(place.address_components[1] && place.address_components[1].short_name || ''),
(place.address_components[2] && place.address_components[2].short_name || '')
].join(' ');
}
infowindowContent.children['place-icon'].src = place.icon;
infowindowContent.children['place-name'].textContent = place.name;
infowindowContent.children['place-address'].textContent = address;
infowindow.open(map, marker);
});
// Sets a listener on a radio button to change the filter type on Places
// Autocomplete.
function setupClickListener(id, types) {
var radioButton = document.getElementById(id);
radioButton.addEventListener('click', function() {
autocomplete.setTypes(types);
});
}
setupClickListener('changetype-all', []);
setupClickListener('changetype-address', ['address']);
setupClickListener('changetype-establishment', ['establishment']);
setupClickListener('changetype-geocode', ['geocode']);
document.getElementById('use-strict-bounds')
.addEventListener('click', function() {
console.log('Checkbox clicked! New state=' + this.checked);
autocomplete.setOptions({strictBounds: this.checked});
});
}
解决方案
Google 的问题跟踪器中报告了此问题:
https://issuetracker.google.com/issues/138267513
它已在 Firefox v68.0.2 中修复。
另见相关:
google-places bug in firefox browser
Embedded Google Maps tiles not loading in Firefox second page load until zoomed
推荐阅读
- java - 由于 ERROR_GSTREAMER_PIPELINE_STATE_CHANGE,javaFX 媒体在循环中崩溃
- c# - 在c#中使用连续的比较运算符
- numpy - 一维上有多个softmax?
- c - 如何在 c 中编写一个函数 char* 来返回按长度排序的单词?
- javascript - 我正在尝试将多个图像下载为 zip 文件,但使用 javascript 获取空文件夹
- react-native - React-Native - 无法使用 GradleScopeCompileServices.configure() 配置服务
- data-conversion - 如何将线性刻度转换为对数刻度
- python - 为什么我的表单验证在 django 中不起作用
- qt-creator - QT:“这个文件不是任何项目的一部分”
- google-cloud-platform - 使用 dtmf 响应从对话流中响应(播放数字)