javascript - 谷歌地图 v3 不是 LatLngBounds 或 LatLngBoundsLiteral:不是对象名称:“InvalidValueError”
问题描述
javascript 不是最好的,并且在一些谷歌地图代码上遇到了一些问题,但它在 MS Edge 上是零星的,但在 Chrome 上是一致的,并且标记不会加载。如果您保持请求页面有时它会工作,但通常会失败。
错误:不是 LatLngBounds 或 LatLngBoundsLiteral:不是对象名称:“InvalidValueError”
使用以下代码调用脚本
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=XXXXXXXXXXXXXXXXXXXXXX&libraries=places"></script>```
<script async type="text/javascript" src="{{media url='scripts/store-locator.js'}}"></script>```
require(["jquery"], function ($) {
$(document).ready(function () {
initialize();
});
var geocoder,
map,
markers = [],
infowindow = new google.maps.InfoWindow(),
bounds = new google.maps.LatLngBounds(),
marker, i;
var locations = [
{ id: 1, name: 'Store1', lat: 58.482514, lng: -1.784622 },
{ id: 2, name: 'Store2', lat: 54.687925, lng: 0.312584 }
];
function initialize() {
// set the default google map settings
geocoder = new google.maps.Geocoder();
directionsDisplay = new google.maps.DirectionsRenderer({ draggable: true });
var latlng = new google.maps.LatLng(52.727440, -1.543299);
var myOptions = {
zoom: 6,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
// register the google map elements
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
// register the directions display
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('directions'));
addMarkers();
map.fitBounds(bounds);
google.maps.event.addListenerOnce(map, 'tilesloaded', function () {
// setup control position
control = document.getElementById('store-selector');
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(control);
control.style.display = 'block';
});
}
// add the markers from the array to the map
function addMarkers() {
$.each(locations, function (key, value) {
// create the marker
marker = new google.maps.Marker({
position: new google.maps.LatLng(this.lat, this.lng),
map: map,
name: this.name
});
// add the marker to the cached array
markers.push(marker);
bounds.extend(marker.position);
// add the event listerner for the marker click function
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
// info window display
map.setZoom(16);
map.setCenter(marker.getPosition());
}
})(marker, i));
});
}
});```
解决方案
好的,我想我已经解决了,这要感谢 geocodezip 提出的时间问题。出错 $(document).ready(function () { initialize(); });
我已将其移至文件底部,现在似乎运行良好..
推荐阅读
- jquery - 如何在 jQuery 中为选择标签应用边框类?
- css - CSS:让文本从场景中永久消失?
- reactjs - React Native Redux 一起使用 state props 和 selectors
- python - 如何有效地重新编码?
- python - 无法减少为纸浆添加约束所花费的时间
- c - 如何在 MICROCHIP XC8 的库中正确使用 __delay_ms()?
- c++ - 如何使用 Lua 从表中的表中获取值?
- javascript - Adyen 自定义卡片标签导航在最新版本中无法正常工作
- javascript - Next-Pwa 无法在 Nginx 等生产服务器上运行
- cakephp - 在 CakePHP 中创建搜索表单