根据这个指南:https://developer.tomtom.com/blog/build-different/adding-tomtom-maps-django-app-building-front-end-map,我正在尝试使用tomtom作为我的django项目,一切都很好,除了缩放部分,地图应该缩放根据我给它的结果。我得到的consol日志错误是:
Uncaught (in promise) Error: `LngLatLike` argument must be specified as a LngLat instance, an object {lng: <lng>, lat: <lat>}, an object {lon: <lng>, lat: <lat>}, or an array of [<lng>, <lat>]我试图运行的代码:
// create the map
tt.setProductInfo('Google Map', '1.0');
let map = tt.map({
key: '',
container: 'map'
});
// add store markers
let bounds = []
let storeLocations = JSON.parse("{{ locations|escapejs }}");
for (let storeLocation of storeLocations) {
let coordinates = [storeLocation.longitude, storeLocation.latitude];
bounds.push(coordinates);
// create popup to display store information when the marker is clicked
let popup = new tt.Popup().setHTML(`
<div class="locator-popup">
<h6>Store Name</h6>
<p>${storeLocation.name}</p>
<h6>Address</h6>
<p>${storeLocation.address}</p>
</div>
`);
let marker = new tt.Marker()
.setLngLat(coordinates)
.setPopup(popup)
.addTo(map);
}
// zoom the map to fit all markers
map.on('load', () => {
console.log(bounds)
map.fitBounds(bounds, {
padding: { top: 50, bottom:50, left: 50, right: 50 }
});
})注:我在想,这可能是因为“界限”。因此,检查了‘console.log(界)’的输出,结果是:
[Array(2)]
0: (2) [51.34912743809577, 35.701243277665895]
length: 1
lastIndex: (...)
lastItem: (...)
[[Prototype]]: Array(0)在加载时,地图应该缩放。
发布于 2022-04-07 10:01:25
fitBounds()需要两个拐角-西南点和东北点。这个错误或多或少说明它没有得到LngLatLike参数。但事实上,它需要其中的两个。
因此,要修复代码--如果数组中只有一个存储,那么可以在边界数组中复制该位置。
类似于:
if (bounds.length = 1) {
bounds.push(bounds[0])
}https://stackoverflow.com/questions/71742741
复制相似问题